Preliminary understanding and simple use cases of webService

1. What is webService?

Web Service is also called XML Web Service Web Service is a lightweight independent communication technology that can receive requests from other systems on the Internet or Intranet. Yes: Software services provided on the Web via SOAP, described using WSDL files, and registered via UDDI.

A few basic concepts:

SOAP: Simple Object Access Protocol, a communication protocol for webService, a specification for calling methods in the form of xml documents, and can support different underlying interfaces, such as http;

WSDL: is an xml document that describes a set of soap messages and how to exchange these messages;

UDDI: It is a new project mainly aimed at Web service providers and users. It is a mechanism to guide clients to find response services based on wsdl files (description documents). It uses SOAP message mechanism (standard XML/HTTP) to publish, edit, browse and find registration information. It uses XML format to encapsulate various types of data, and sends it to the registration center or returns the required data by the registration center.

In a word: webService is a typical and mature technical specification for remote service invocation. Such as the access of the website to the weather forecast interface, the interface access to the train schedule and flight, etc.

 

Second, the simple example of the server

1) Server-side service interface simulation

Looking at the previous examples on the Internet, one interface service corresponds to one wsdl file, but I considered: what if there is more than one interface? What to do? ——Actually, of course, I can have one interface implementation class corresponding to one wsdl file, and I have tested it without any problem, but now I want to publish multiple interface services, but only generate one wsdl file.

To achieve this effect is very simple, just use one implementation class that implements several interfaces at the same time. But this will cause coupling (we will test this below. In specific applications, generally a certain type of service corresponds to an interface. At this time, we still need to use a mode in which an interface corresponds to a wsdl file.)

 

Interface one:

@WebService
public interface IService {
	
	public void sayHello(@WebParam(name="username") String name);
	
	public int calculatr(int m, int n);
}

 Interface two:

@WebService
public interface IService2 {
	public void getInfo(@WebParam(name="person") Person person);
}

Among them, the Person class is a pojo,

public class Person implements Serializable{//Because it is used for remote transmission, it needs to be serialized
	
	private static final long serialVersionUID = 1L;
	private String name;
	private int age;
	private String gender;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	@Override
	public String toString() {
		return "Pserson [name=" + name + ", age=" + age + ", gender=" + gender
				+ "]";
	}
	
}

  The implementation class of the interface (here I test 2 interfaces corresponding to 1 implementation class, that is, generate a wsdl file, but this may not be recommended in reality..)

@WebService //Note: The implementation class must have this annotation before it can be published
public class MutiServiceImpl implements IService, IService2 {

	@Override
	public void getInfo(Person person) {
		System.out.println("The person's information: " + person);
	}

	@Override
	public void sayHello(String name) {
		System.out.println("Hello, my name is " + name);
	}

	@Override
	public int calculatr(int m, int n) {
		return m * n;
	}

}

 The release of the server interface service:

public class ServicePublish {
	
	public static void main(String[] args) {
		String address = "http://localhost:9001/Service/service";
//address is specified by yourself, but it must conform to the specification http://ip address: port/service name/specific service
//The second parameter must be a concrete implementation class
		Endpoint.publish(address, new MutiServiceImpl());
		System.out.println("WebService's server service was released successfully!");
	}
	
}

 Run the main method, you can see the print information, indicating that the interface service is successfully released! (In fact, testing webService does not have to create a web project, just an ordinary project~~)

 

We might as well enter the just-published address on the browser: http://localhost:9001/Service/service?wsdl (to add ?wsdl), you can see an automatically generated description file in xml format:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<!--
 Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://the_service/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://the_service/" name="MutiServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://the_service/" schemaLocation="http://localhost:9001/Service/service1?xsd=1"/>
</xsd:schema>
</types>
<message name="getInfo">
<part name="parameters" element="tns:getInfo"/>
</message>
<message name="getInfoResponse">
<part name="parameters" element="tns:getInfoResponse"/>
</message>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<message name="calculatr">
<part name="parameters" element="tns:calculatr"/>
</message>
<message name="calculatrResponse">
<part name="parameters" element="tns:calculatrResponse"/>
</message>
<portType name="MutiServiceImpl">
<operation name="getInfo">
<input wsam:Action="http://the_service/MutiServiceImpl/getInfoRequest" message="tns:getInfo"/>
<output wsam:Action="http://the_service/MutiServiceImpl/getInfoResponse" message="tns:getInfoResponse"/>
</operation>
<operation name="sayHello">
<input wsam:Action="http://the_service/MutiServiceImpl/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://the_service/MutiServiceImpl/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
<operation name="calculatr">
<input wsam:Action="http://the_service/MutiServiceImpl/calculatrRequest" message="tns:calculatr"/>
<output wsam:Action="http://the_service/MutiServiceImpl/calculatrResponse" message="tns:calculatrResponse"/>
</operation>
</portType>
<binding name="MutiServiceImplPortBinding" type="tns:MutiServiceImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="getInfo">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="calculatr">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="MutiServiceImplService">
<port name="MutiServiceImplPort" binding="tns:MutiServiceImplPortBinding">
<soap:address location="http://localhost:9001/Service/service1"/>
</port>
</service>
</definitions>

Here, by the way, briefly talk about some key fields of this wsdl file.

1) definitions element - the root element of the wsdl file

2) type element - as a container, it defines a custom special data type. When declaring the message part (payload), the messages definition uses the data type and element defined in the types element.

3) The Message element describes the payload of the Web service. Equivalent to parameters and return values ​​in a function call.

4) The PortType element defines the abstract interface of the Web service, which can consist of one or more operation elements, each of which defines an RPC-style or document-style Web service method, which is equivalent to the name of the class interface.

5) The Operation element uses one or more messages to define its input, output and errors, which are equivalent to the functions contained in the interface.

6) The Binding element maps an abstract portType to a specific set of protocols (SOAP or HTTP), messaging styles (RPC or document), and encoding styles (literal or SOAP encoding).

7) The Service element contains one or more Port elements, each Port element corresponds to a web service

 

3. Simple example of the client (open another project, not necessarily a web project)

After generating the wsdl file, we need to [automatically] generate the calling code of the client according to the wsdl file.

Generally speaking, it is simpler to use the commands that come with Jdk1.6+. (The premise is that you have configured the jdk environment variable)

The specific command format is:

wsimport -s "src directory" -p "package name where the generated class is located" -keep "wsdl release address"

In this example, wsimport -s E:\xxx\the_client\src -p org.webservice.client -keep http://localhost:9001/Service/service?wsdl (because both the server and client are on the same machine machine, so use localhost directly. If it is not the same machine, the client needs to enter the address of the server exactly)

After the command is successfully executed, the refresh will find the following classes:



 Next, we start the remote call test:

public class ClientSub {
	public static void main(String[] args) {
		try {
			MutiServiceImpl impl = new MutiServiceImplService().getMutiServiceImplPort();
			
			int x = impl.calculatr(30, 2);
			System.out.println("Calculation result: " + x);
			
			Person person = new Person();
			person.setAge(23);
			person.setGender("男");
			person.setName("Karl");
			impl.getInfo(person);
			
			impl.sayHello("Connor");
			
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}
}

Running the subscription class of the service, you will find that the consoles of the server and client are printed as follows: 

Server:



 Client:



 

Description: The use of webService to remotely call the interface service is successful!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326828012&siteId=291194637