Summary of WebService learning -- using JDK to develop webservice

Summary of WebService learning (2) -- using JDK to develop webservice
1, Development means of WebService
When using Java to develop web services, you can use the following two development methods

1. Development with JDK (version 1.6 and above)

2. Develop with CXF framework (in work)

2, Using JDK to develop WebService
2.1 develop WebService server
1. Define an interface, annotate the interface with @ WebService annotation, and annotate all methods defined in the interface with @ WebMethod annotation, as follows:
 

package me.gacl.ws;
 
import javax.jws.WebMethod;
import javax.jws.WebService;
 
/**
 * @author gacl
 * Define SEI (WebService endpoint interface)
 */
//Annotate WebServiceI interface with @ WebService annotation
@WebService
public interface WebServiceI {
 
    //Annotate methods in WebServiceI interface with @ WebMethod annotation
    @WebMethod
    String sayHello(String name);
 
    @WebMethod
    String save(String name,String pwd);
}

2. Write the implementation class of interface, and use @WebService Annotate the implementation class to implement all the methods defined in the interface, as follows:

package me.gacl.ws;
 
import javax.jws.WebService;
 
/**
 * @author gacl
 * SEI The concrete realization of
 */
//Annotate the implementation class WebServiceImpl of WebServiceI interface with @ WebService annotation
@WebService
public class WebServiceImpl implements WebServiceI {
 
    @Override
    public String sayHello(String name) {
        System.out.println("WebService sayHello "+name);
        return "sayHello "+name;
    }
 
    @Override
    public String save(String name, String pwd) {
        System.out.println("WebService save "+name+", "+pwd);
        return "save Success";
    }
}

3. Use the endpoint class to publish the webservice. The code is as follows:

package me.gacl.ws.test;
 
import javax.xml.ws.Endpoint;
 
import me.gacl.ws.WebServiceImpl;
 
/**
 * @author gacl
 *
 * Publish Web Service
 */
public class WebServicePublish {
 
    public static void main(String[] args) {
        //Define the publishing address of the WebService, which is the URL address provided to the outside world to access the WebService. The format of the URL address is: http://ip : Port No. / xxxx
        //String address = "http://192.168.1.100:8989 / "; this WebService publishing address is legal
        //String address = "http://192.168.1.100:8989/Webservice "; this WebService publishing address is legal
        String address = "http://192.168.1.100:8989/WS_Server/Webservice";
        //Use the publish method provided by the Endpoint class to publish the WebService. When publishing, make sure that the port number used is not occupied by other applications
        Endpoint.publish(address , new WebServiceImpl());
        System.out.println("release webservice success!");
    }
}

Run WebServicePublish class to publish the prepared WebService. The access URL of WebService is: http://192.168.1.100:8989/WS_Server/Webservice, as shown in the following figure:

  

Here we write a WebServicePublish class to publish WebService. If it is a Web project, we can use a listener or Servlet to publish WebService, as follows:

1. Use ServletContextListener listener to publish WebService
 

package me.gacl.listener;
 
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.xml.ws.Endpoint;
import me.gacl.ws.WebServiceImpl;
 
/**
 * @author gacl
 * Listener for Publishing Web Services
 */
//Use the @ WebListener annotation provided by Servlet3.0 to mark the WebServicePublishListener class that implements the ServletContextListener interface as a Listener
@WebListener
public class WebServicePublishListener implements ServletContextListener {
 
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
 
    }
 
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //Publishing address of WebService
        String address = "http://192.168.1.100:8080/WS_Server/WebService";
        //Publish WebService. WebServiceImpl class is the concrete implementation class of WebServie interface
        Endpoint.publish(address , new WebServiceImpl());
        System.out.println("use WebServicePublishListener release webservice success!");
    }
}

When the Web application is deployed to the server runtime, the Web service will be published when the Web application context is initialized.

Then we can use the published URL address to access the WebService, as shown in the following figure:

  

2. Use Servlet to publish WebService

package me.gacl.web.controller;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.xml.ws.Endpoint;
import me.gacl.ws.WebServiceImpl;
 
/**
 * @author gacl
 * Servlet for publishing WebService
 */
//Use the @ WebServlet annotation provided by Servlet 3.0 to mark a normal Java class that inherits the HttpServlet class as a Servlet
//Set the value property to an empty string so that WebServicePublishServlet does not provide a path for external access
//The loadOnStartup property sets the initialization time of WebServicePublishServlet
@WebServlet(value="",loadOnStartup=0)
public class WebServicePublishServlet extends HttpServlet {
 
    /* (non-Javadoc)
     * @see javax.servlet.GenericServlet#init()
     * Publish WebService on WebServicePublishServlet initialization
     */
    public void init() throws ServletException {
        //Publishing address of WebService
        String address = "http://192.168.1.100:8888/WebService";
        //Publish WebService. WebServiceImpl class is the concrete implementation class of WebServie interface
        Endpoint.publish(address , new WebServiceImpl());
        System.out.println("use WebServicePublishServlet release webservice success!");
    }
}

When the Web application is deployed to the server runtime, when the WebServicePublishServlet is initialized, the WebService will be published. As shown in the figure below:

  

Then we can use the published URL address to access the WebService, as shown in the following figure:

  

The main way to publish Web services is through javax.xml.ws.Endpoint class provides a static method publish for publishing. If it is a normal java project, you can write a special class for publishing WebService. If it is a Web project, you can use ServletContextListener or Servlet for publishing.

2.2 develop WebService client
1. With the help of JDK's wsimort.exe Tools generate client code, wsimort.exe The tool is located in the bin directory of JDK, as shown in the following figure:

  

Execute the command: wsimport -keep url(url is the path of the wsdl file) to generate the client code.

Create a WebService client test project, as shown in the following figure:

  

Open the command line window, switch to the src directory, and execute "wsimport -keep" http://192.168.1.100:8888/WebService?wsdl "generates client code, as shown in the following figure:

  

If no error occurs during the execution of the command, the code is generated successfully. Refresh the src directory to see the generated code, as shown in the following figure:

  

2. Call the methods provided by WebService with the help of generated code

wsimport tool helps us generate several java classes, but we only need to care about the use of WebServiceImplService class and WebServiceImpl interface, as shown below:

package me.gacl.ws.client;
 
import me.gacl.ws.WebServiceImpl;
import me.gacl.ws.WebServiceImplService;
 
/**
 * @author gacl
 * Clients calling WebService
 */
public class WSClient {
 
    public static void main(String[] args) {
        //Create a factory to generate WebServiceImpl instances. The WebServiceImplService class is generated by wsimport tool
        WebServiceImplService factory = new WebServiceImplService();
        //A WebServiceImpl instance is generated through the factory, which is generated by wsimport tool
        WebServiceImpl wsImpl = factory.getWebServiceImplPort();
        //Call sayHello method of WebService
        String resResult = wsImpl.sayHello("Struggling shrimp");
        System.out.println("call WebService Of sayHello The result of the method is:"+resResult);
        System.out.println("---------------------------------------------------");
        //Call the save method of WebService
        resResult = wsImpl.save("Struggling shrimp","123");
        System.out.println("call WebService Of save The result of the method is:"+resResult);
    }
}

The client calls the WebService method of the server to run;

The result from the call shows that the client code generated by wsimport tool has successfully called the method in WebService. The above is related to the development of Web services using JDK.

Keywords: JDK Java xml

Added by po on Mon, 15 Jun 2020 07:24:40 +0300