Recently, I have used WebService in my work, but I don’t know much about it, so I plan to learn it systematically.

What is?

Web services is a service-oriented architecture (SOA) technology that provides services through standard Web protocols to ensure that application services from different platforms can interoperate. According to the W3C definition, a Web service should be a software system that supports the interaction between different machines on a network. Web services are typically composed of many application program interfaces (apis) that perform requests for services submitted by customers over a network, such as the remote server side of the Internet. — Wikipedia

From the wikipedia description above we can extract two key points:

  • It’s a technology
  • Services are provided through standard Web protocols
  • Ensure that application services on different platforms can operate with each other

The platform here can be understood in different programming languages so we can think of Web services as something like this, right?

So the question is, right? Isn’t it good to just use HTTP? HTTP is also cross-platform. With that in mind, I scroll down to the definition of WebService:

Although the W3C definition covers many disparate and indistinguishable systems, we generally refer to the transfer of XML-formatted messages between client-servers under the SOAP protocol

This master-slave architecture is a bit of a mouthful, but if you look at the client-server architecture, it will be easier to understand the client-server architecture. One of the key points we can extract from this description is the delivery of XML-formatted messages under the SOAP protocol. Here’s a strange word SOAP. SOAP is also one of the four components of WebService:

  • The basics of XML SOAP sending messages
  • Simple Object Access Protocol (SOAP)
  • WebService Description Language (WSDL) WebService Description Language
  • UDDI (UnviversalDescription, Discovery, andIntegration) universal description Discovery and integration agreement

The meanings of these words will be explained below.

What is SOAP?

SOAP (an acronym for Simple Object Access Protocol) is a Protocol specification for exchanging data. It is used in Web Services on computer networks to exchange structured information. SOAP makes it easier for Web servers to extract data from XML databases, save formatting time, and exchange data in XML format between different applications based on HTTP communication protocol, making it abstract in language implementation, platform, and hardware. Developed in 1998 by IBM, Microsoft, UserLand, and DevelopMentor, and supported by IBM, Lotus, Compaq, and others, It was submitted to the World Wide Web Consortium (W3C) in 2000. SOAP version 1.1 is a common standard in the industry and belongs to the second generation of XML protocol (xmL-RPC and WDDX are the main representative technologies of the first generation). — Wikipedia

So SOAP is another protocol for the application layer? Level with HTTP? HTTP is an acronym for Hyper Text Transfer Protocol. Transfer stands for Transfer. SOAP does not have this T, so it is not the same level of protocol as HTTP. You forgot WebSocket also does not come with transfer. Here is just a hint.

One of the key descriptions is “data interchange between different applications using HTTP communication protocols and XML format”. So SOAP still uses HTTP protocol for transport, so what does SOAP protocol say? Note the “XML format compliance “, so we can assume that SOAP specifies the XML format and that requests sent when exchanging information using HTTP are ALL XML.

So in summary, when WebService can send request and receive result through HTTP protocol, the sent request content and result content are encapsulated in XML format, and some specific HTTP request headers are added. Used to describe the content format for requesting and sending HTTP messages, these specific HTTP headers and XML content formats are the SOAP protocol.

The address of a WebService is as follows:

  • Ws.webxml.com.cn/WebServices…

Let’s use a browser to look at the message returned by a typical WebService:

WSDL

If I don’t understand your Web Service, refer to WSDL, which provides “documentation of what can be done “: a description format for the Service to be provided. I want to help you, but I’ll tell you what I can do and the types of parameters needed to do it. In WSDL, WSDL: WebService Description Language WebService Description Language. A WSDL file is an XML document that describes a set of SOAP messages and how to exchange them, that is, where the current service is, what interfaces can be called, what methods, what parameters, and so on. A Web Service corresponds to a unique WSDL document.

UDDI

UDDI: Universal description of discovery and integration protocols. UDDI is a directory service that businesses can use to register and search for Web services. This can be understood as the existence of a registry in a microservice. If there are many Published WebService services, then the service publication can be registered in UDDI.

Just to summarize a little bit

So in summary we can think of WebService as:

Develop WebService service

So much has been said above, we now come to the development of WebService service, the development of WebService service commonly used in the following ways:

  • The JDK publishes the request WebService service
  • Use CXF to develop Web services
  • AXIS develops and publishes WebServices

The JDK development WebService

First, develop the server side

First write a service, first write an interface:

WebService
public interface HelloWebService {
    String sayHelloWorld(a);
}
Copy the code
@WebService
public class HelloWebServiceImpl implements HelloWebService {
    @Override
    public String sayHelloWorld(a) {
        return "hello bbbb"; }}Copy the code

Then publish the service in the main function:

public class MyWebServiceServer { public static void main(String[] args) { Endpoint.publish("http://localhost:8080/hello",new HelloWebServiceImpl()); System.out.println("webService started......." ); }}Copy the code

Write the client again

Wsimport in the JDK automatically generates the client code for you, but you need to configure the environment variables first. After the configuration is complete, run the following command:

wsimport -keep http://localhost:8081/testWebService? wsdl

-encoding: specifies the encoding format. -keep: specifies whether to generate Java source files

-d: specifies the output directory of the. Class file

-s: specifies the output directory of the. Java file

-p: defines the package name of the generated class. If not, the package name is default. -verbose: displays output information on the console

-b: Specifies jaxWS/JAXB binding files or additional schemas

-extension: uses the extension to support SOAP1.2Calling tests:

public class MyClient {
    public static void main(String[] args) {
        org.example.webservice.HelloWebServiceImplService hss = neworg.example.webservice.HelloWebServiceImplService(); org.example.webservice.HelloWebServiceImpl helloWorld = hss.getHelloWebServiceImplPort(); System.out.println(helloWorld.sayHelloWorld()); }}Copy the code

Apache CXF development and parsing

The first step is to introduce dependencies:

<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.3.5</version>
</dependency>
<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.3.5</version>
</dependency>
Copy the code
public interface LoginService {
  String sayHello(a);
}
@WebService
public class LoginServiceImpl implements LoginService {
    @Override
    public String sayHello(a) {
        return "hello"; }}public class CxfWebServiceServer {
    public static void main(String[] args) {
        JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
        jaxWsServerFactoryBean.setAddress("http://localhost:8081/cxf_server");
        jaxWsServerFactoryBean.setServiceClass(LoginServiceImpl.class);
        Server server = jaxWsServerFactoryBean.create();
        server.start();
        System.out.println("server start"); }}Copy the code

Generate client code

  • Use the wsimport generates

Same thing as above

The Axis resolution WebService

Download AxIS-1.4 first and unzip it to:Archive.apache.org/dist/ws/axi… Fill in the bat file with the following instructions:

Set axis_lib=E:\360\axis-bin-1_4\axis-1_4\lib // Here fill in the decompressed Axislib path set java_cmd= java-djava.ext. dirs=%axis_lib% set axis_servlet=http://localhost:8081/cxf_server? wsdl %java_cmd% org.apache.axis.wsdl.WSDL2Java -u %axis_servlet% pauseCopy the code

Call example:

public class TestDemo {
    public static void main(String[] args) throws Exception {
        LoginServiceImplServiceLocator loginServiceImplServiceLocator = new LoginServiceImplServiceLocator();
        LoginServiceImplServiceSoapBindingStub bindingStub = new LoginServiceImplServiceSoapBindingStub(new URL(loginServiceImplServiceLocator.getLoginServiceImplPortAddress()),neworg.apache.axis.client.Service()); System.out.println(bindingStub.sayHello()); }}Copy the code

To summarize

At the beginning of the writing, I always want to write all the WebService related things in a rush, but later it is too long and a burden to the reader. Let’s just break it down. In the first installment, we will focus on using it, understanding the concept of WebService, and simply publishing and invoking services using class libraries. In the introduction, we’ll talk about more advanced applications.

The resources

  • REST versus SOAP for Web Services
  • UDDI (Universal Description, Discovery, and Integration Protocol) Summary Author: I eat Indian pancakes