Basic use of WebService

WebService definition : As the name suggests, it is a Web-based service. It uses the Web (HTTP) method to receive and respond to certain requests from external systems. So as to realize the remote call.

WSDL  – WebService Description Language – Web Services Description Language.

Indicates where the service is located in XML form - the address.

Describe in XML what methods the service provides - how to invoke it.

A specification provided by the server to the client

    Constrains the message format for communication between client and server

SOAP -Simple Object Access Protocol (Simple Object Access Protocol)

SOAP is an XML-based protocol for transferring data over the Internet.

SOAP = Based on HTTP + XML data.

SOAP is based on HTTP.

The composition of SOAP is as follows:

Envelope - Required part. Appears as the root element of XML.

Headers - optional.

Body - Required. In the body section, contains the server method to execute. and data sent to the server.

1. Development of server-side programs

1 @WebService // This annotation must be added when publishing services with jdk 
2  public  class PersonService {
 3      
4      public String sayHello(String name){
 5          return name + " hello" ;
 6      }
 7 }
1  public  class Publisher {
 2      public  static  void main(String[] args) {
 3          // The first parameter: the address of the publishing service
 4          // The second parameter: the object to create the service class 
5          Endpoint.publish("http: //192.168.22.1:8099/hello", new PersonService());
 6      }
 7 }

Use Endpoint to publish Endpoint.publish("http://192.168.15.82:8098/hell", new UserServiceImpl()); Note: The service class of the service class webservice must have at least one method jdk when publishing the webservice service. You must add @webService to the method of annotation service class 1. It cannot be static * 2. It cannot be final JDK does not support webservice publishing service classes with interfaces, and it also has defects in soap1.2 protocol

Second, the client invokes the program

wsimport: Parameters: -s generate source code -p package name after generating code

Steps: (1) Find an empty folder through the command line, switch to the empty folder directory and execute: wsimport -s . -p com.xxx.xx http://192.168.15.82:8099/hello?wsdl

(2) Copy the code to the client's project

(3) Server call

   //1. Create a service access point collection object PersonServiceService pss = new PersonServiceService();

  //2. Get the class bound to the service point, use get plus the name of the port (PersonServicePort), getPersonServicePort PersonService ps = pss.getPersonServicePort();

  //3. Call the server-side method String result = ps.sayHello("zhangsan"); System.out.println(result);

 

Guess you like

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