I would like to talk to you about WebService recently. In my work, I often work with some third-party protocols, many of which are WebService protocols. So next, let’s have an in-depth understanding of this technology, starting from the basic concepts and principles, and then talking about examples.
We’re going around what is a WebService, how does a WebService work, how does a WebService work? These three aspects to elaborate.
Creation is not easy, remember to like, follow, bookmark yo. I hope you can read it carefully, it should be helpful to your work.
What is the WebService
WebService is a cross-programming language, cross-operating system platform remote call technology.
Remote call technique: A remote call is when A program on one device can call method B on another device. For example, the weather forecast system, Taobao.com, Xiaonei and Baidu have exposed their system services in the form of WebService so that third-party websites and programs can call these service functions, thus expanding their market share.
Cross-programming language: it means that the programming languages of the server and client programs can be different.
Cross-os platform: The server and client can run on different OPERATING systems.
So that’s what we do that’s why we use it, rightWebService
The reason why.WebService
Can solve: cross-platform call, cross-language call, remote call.
WebService principle
WebService follows SOAP protocol and encapsulates data through XML, and then transmits data through Http.
The WebService server first uses a WSDL file to describe what services it can call. In simple terms, WSDL is like an specification that describes a WebService and its methods, parameters, and return values. The WSDL file is stored on a Web server and can be accessed through a URL address.
Before a client invokes a WebService, it needs to know the address of the WSDL file for the service.
A WebService provider can expose its WSDL file address in two ways: 1. Register with a UDDI server so that it can be looked up by others; 2. Tell the client caller directly.
The SOAP protocol
SOAP is a simple XML-based protocol that enables applications to exchange information over HTTP. Or to put it more simply: SOAP is a protocol for accessing web services.
The basic structure of SOAP messages
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>.</soap:Header>
<soap:Body>.<soap:Fault>.</soap:Fault>
</soap:Body>
</soap:Envelope>
Copy the code
A SOAP message is a plain XML document containing the following elements:
- Required Envelope element to identify this XML document as a SOAP message
- An optional Header element that contains Header information
- The required Body element, which contains all the call and response information
- An optional Fault element that provides information about the error that occurred in processing this message
WSDL
WSDL is an XML-based language for describing Web Services and how to access them.
WebService protocol documents
Custom WebService service
Apache CXF is an open source Services framework that helps you build and develop Services that support a variety of protocols, such as: SOAP, POST/HTTP, and RESTful HTTP CXF simplify services so that they naturally integrate seamlessly with Spring.
Create a Maven project to introduce dependencies
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.14.</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.14.</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.14.</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.1820.</version>
</dependency>
Copy the code
Write an entity class Car
@Data
public class Car
{
private String brand;/ / brand
private String plateNum;/ / license plate number
private double price;// Price unit: 10,000
private String owner;/ / owner
}
Copy the code
Interface code writing
package com.cn.service;
import com.cn.domain.Car;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(targetNamespace = "http://service.cn.com/")
public interface CarService
{
/** * 1. Add to the class@WebServiceAnnotation, representing publishing a WebService service. * 2. Add to the class@WebServiceAfter the annotation, all non-static methods in the class will be published. * 3. If you want a method to be private, add it to the method@WebMethodExclude =true; exclude=true; * 4. If, on a class, is added@WebServiceAnnotation, the class must have at least one method that can be exposed, otherwise the startup will fail. * Protected, private, final, and static methods cannot be made public. * 5.@targetNamespaceSet the namespace, default package name, take the */
@WebMethod
public Car getCarInfo(@WebParam(name = "carBrand", targetNamespace = "http://service.cn.com/") String carBrand);
}
Copy the code
The implementation class of the interface
package com.cn.service;
import com.cn.domain.Car;
public class CarServiceImpl implements CarService
{
@Override
public Car getCarInfo(String carBrand)
{
Car car = new Car();
if (carBrand.equals("audi"))
{
car.setBrand("audi");
car.setOwner("Tom");
car.setPlateNum("Anhui A11011");
car.setPrice(50.00);
}
else if (carBrand.equals("bmw"))
{
car.setBrand("bmw");
car.setOwner("Jack");
car.setPlateNum("Anhui A8888M");
car.setPrice(60.00);
}
else
{
car.setBrand("Other");
car.setOwner("Rose");
car.setPlateNum("Unknown");
car.setPrice(00.00);
}
returncar; }}Copy the code
Service release
package com.cn;
import com.cn.service.CarServiceImpl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class AppTest
{
public static void main(String[] args)
{
// The factory that publishes the service
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
// Set the address of the service
factory.setAddress("http://127.0.0.1:9999");
// Set the service class
factory.setServiceBean(new CarServiceImpl());
// Publish the service
factory.create();
System.out.println("The service was published successfully. The WSDL address of the service is: http://127.0.0.1:9999? wsdl"); }}Copy the code
After successful publication, you can enter the url http://127.0.0.1:9999? wsdl
<wsdl:definitions name="CarServiceImplService" targetNamespace="http://service.cn.com/">
<wsdl:types>
<xs:schema elementFormDefault="unqualified" targetNamespace="http://service.cn.com/" version="1.0">
<xs:element name="getCarInfo" type="tns:getCarInfo"/>
<xs:element name="getCarInfoResponse" type="tns:getCarInfoResponse"/>
<xs:complexType name="getCarInfo">
<xs:sequence>
<xs:element minOccurs="0" name="carBrand" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getCarInfoResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:car"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="car">
<xs:sequence>
<xs:element minOccurs="0" name="brand" type="xs:string"/>
<xs:element minOccurs="0" name="owner" type="xs:string"/>
<xs:element minOccurs="0" name="plateNum" type="xs:string"/>
<xs:element name="price" type="xs:double"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="getCarInfoResponse">
<wsdl:part element="tns:getCarInfoResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="getCarInfo">
<wsdl:part element="tns:getCarInfo" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="CarService">
<wsdl:operation name="getCarInfo">
<wsdl:input message="tns:getCarInfo" name="getCarInfo"> </wsdl:input>
<wsdl:output message="tns:getCarInfoResponse" name="getCarInfoResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CarServiceImplServiceSoapBinding" type="tns:CarService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getCarInfo">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getCarInfo">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getCarInfoResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CarServiceImplService">
<wsdl:port binding="tns:CarServiceImplServiceSoapBinding" name="CarServiceImplPort">
<soap:address location="http://127.0.0.1:9999/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
Copy the code
Client call
There are many ways to call the client side, here I will not introduce one, I will talk about my usual work with the way.
Soap client -SoapClient
In interface docking, WebService interface occupies a large share, and in order to use these interfaces, we have to introduce libraries like Axis to realize interface requests.
The client introduces dependencies
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.43.</version>
</dependency>
Copy the code
Now with Hutool, you can implement simple WebService requests without any dependencies.
From the WSDL document above, we know:
- Method name:
getCarInfo
- There is only one parameter:
carBrand
- Defines a namespace with a URI
http://service.cn.com/
So we can build the appropriate SOAP request:
public class Test
{
public static void main(String[] args)
{
// Set the WebService address
SoapClient client = SoapClient.create("http://127.0.0.1:9999")
// Set the method to be requested, passing in the corresponding namespace
.setMethod("getCarInfo"."http://service.cn.com/")
// Set parameters
.setParam("carBrand"."audi");
// Send a request with the argument true to return a formatted XML content
// Returns an XML string that can be parsed with XmlUtil
String result = client.send(true);
System.out.println(result);
System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
Document docResult = XmlUtil.readXML(result);
Object brand = XmlUtil.getByXPath("//return//brand", docResult, XPathConstants.STRING);
Object owner = XmlUtil.getByXPath("//return//owner", docResult, XPathConstants.STRING);
Object plateNum = XmlUtil.getByXPath("//return//plateNum", docResult, XPathConstants.STRING);
Object price = XmlUtil.getByXPath("//return//price", docResult, XPathConstants.STRING);
System.out.println("brand = " + brand);
System.out.println("owner = " + owner);
System.out.println("plateNum = " + plateNum);
System.out.println("price = "+ price); }}Copy the code
Data returned:
<? xml version="1.0" encoding="UTF-8" standalone="no"? > <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:getCarInfoResponse xmlns:ns2="http://service.cn.com/">
<return> <brand> Audi </brand> <owner>Tom</owner> <plateNum> Anhui A11011</plateNum> <price>50.0</price>
</return> </ns2:getCarInfoResponse> </soap:Body> </soap:Envelope> ---------------------------- brand = audi owner = Tom plateNum = Anhui A11011 price =50.0
Process finished with exit code 0
Copy the code
SoapUI
Link: pan.baidu.com/s/1UIMv_E8x… Extraction code: B9RG
SoapUI is an open source testing tool that uses SOAP/HTTP to examine, invoke, and implement functionality/load/conformance tests for Web services. This graphical tool can also be used to invoke the WebService service for testing purposes.
conclusion
So much for WebService knowledge, in fact, at work, it is enough to know. Understanding the principle of WebService is the most important, there are many small knowledge points I will not repeat here, there are many online information, friends can go to check. Let’s come on together