[Java] Simple understanding of webservice in Java

I recently arrived at a new company and need to develop a webservice interface. I have only developed an interface in the form of http before. After learning some, I will summarize and publish my learning experience.

  1. what is webservice

    I don’t know what the specific bottom layer is. After learning it, my understanding is a way of communicating between a client and a server through sockets. The server publishes the service interface that can be provided, and the client obtains the processing of the interface code that can be used through the wsdl published by the server. class, and then directly pass the parameters into the processing class in the client, and the data returned by the interface can be obtained after the processing class is completed and the response is returned.

  2. how to use webservice

    The general process is:

    • Create a new server server project, create a new server class in the project, the class needs to be annotated @WebService,
    • Then the class needs at least one method with public access, annotated @WebMethod. This method is the method to be called by others
    • Add a main method to the server to start the server service. The location of the server and the objects that can be accessed need to be exposed.

      public static void main(String[] argv) {
          Object implementor = new HelloWorld ();
          String address = "http://localhost:9000/HelloWorld";
          Endpoint.publish(address, implementor);
          System.out.println("publish success!");
      }
    • As above, HelloWorld is equivalent to a controller, a class that can accept external requests, and the address is the address that can be accessed. The webservice is deployed through the publish of the Endpoint. (ps: webservice needs to import the package, please refer to Baidu for the relevant package). The main method cannot be closed , which is equivalent to a server. This ends the server part.

    • Then create a new client client project, create a new client class in the project, and then use the wsdl generated in the server just now to generate the code and call it directly. I use the idea, which can be generated directly by right-clicking:
      Right-click and find WebService at the bottom
      put the address in the main method just now. Finally, add ?wsdl, it becomes http://localhost:9000/HelloWorld?wsdl , this is the generated webservice, you can view the content through the browser, in the form of xml.
      Just fill in the url
      Fill in the url to confirm.
    • However, when I used this method to generate an error, I used cmd, which was generated by the command. The command is as follows
      wsimport -s d:\\workspace\\TheClient\\src -p com.test.client -keep http://localhost:9000/HelloWorld?wsdl

    Among them, -s is followed by the src path of the project that needs code generation, -p is to create a new package to put the generated code, -keep is the source of the code, that is, the address of the previously published webservice.
    After the carriage return is executed, The prompt is executing the code, compiling the code, and then OK. Refresh the client project.

    • Create a new class in the client project and write a main method to test whether the webservice is available

      public static void main(String[] args) {
          HelloWorld helloWorld = new HelloWorldService().getHelloWorldPort();
          System.out.println(helloWorld.sayHelloWorldFrom("paul"));
      }
    • Get the data returned by the server, and the test is successful. This is the complete request and response process of the webservice.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325819113&siteId=291194637