Generation and Call of Webservice Interface

Posted by Bobulous on Sat, 26 Feb 2022 18:42:40 +0100

Recent projects have to dock a web service-style interface, because this type has never been docked before, so this time I have looked up some materials to learn

1. Brief introduction of Webservice

WebService is a remote invocation technology that spans programming languages and operating system platforms. Through the standard communication protocol, it publishes useful program modules as services on the Internet. Currently, most of them use SOAP as their communication protocol. It provides a detailed interface description, called WSDL (Web Service Description Language), to help users build applications. It also has an XML format for both request and return messages.

Advantage:
1. Cross-programming languages and cross-operating systems. In general, as long as your network is connected, you. What I developed with net can also be called in my java language and has nothing to do with the operating system.

Disadvantages:
1. If the service-side interface is a webservice, the client must also use the webservice.
2. Because webservice uses xml to transfer data, high concurrency cannot be satisfied in performance

2. Service-side Publishing of Webservice

1. Publish web application server publishing (this block will write a separate module later)

2. Publish locally (this is really simple, just write a test class and run it, I paste the code directly)

package com.niu;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class TestWebservice {
    /**
     * Methods for client invocation
     * @param name
     * @return
     */
    public String getvalue(String name) {
        return "My name is"+name;
    }
    public static void main(String[] args) {
        //For publishing services, the first parameter is the service address, and the port number needs to be unoccupied. The second parameter is this class
        Endpoint.publish("http://localhost:9002/Service/TestService",
        new TestWebservice());
        System.out.println("service succes");
    }
}

3. Code Generation for Webservice

1. Generate code with dos command (in idea you can open the terminal directly and execute wsimport command if jdk is installed)

(1)wsimport -keep http://xxxx/xxx/xxx.asmx?wsdl

(2)wsimport -s d:\java\src -p com.nrj.client -keep http://xxxx/xxx/xxx.asmx?wsdl

Wsimport-s file path-p package name-keep http://xxxx/xxx/xxx.asmx?wsdl

2.idea generation (right-click open in terminal directly to open terminal)

4. Client Calls to Webservice

1. Simple call (also write a simple test class directly, and put the generated code in the project, then run it)

package com.niu.ws;

import com.niu.TestWebservice;
import com.niu.TestWebserviceService;

public class ServiceTest {
    public static void main(String[] args) {
        TestWebservice ws = new TestWebserviceService().getTestWebservicePort();
        String name = ws.getvalue("Zhang San");
        System.out.println("Output:"+name);
    }
}

5. The xml interface specification of wsdl

<service> service view, the service node of a web service, which includes the service endpoint
<binding> Define message format and protocol details for each service endpoint
<portType> Service endpoint, describing how a web service can be operated, and related messages, pointing to portType through binding s
<message> Defines the data parameters (which can have multiple parameters) for an operation (method)
<types> Define all data types used by the web service

6. Common Web Service interface addresses

IP Address Source Search WEB Services (currently the most complete IP Address Data)
Endpoint :http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx
Disco         :http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?disco
WSDL       :http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl

Random English, Numbers and Simplified Chinese Characters WEB service
Endpoint :http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx
Disco        :http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?disco
WSDL       :http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl

China Postal Code <-> Two-way Query of Address Information/search WEB service
Endpoint :http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx
Disco        :http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx?disco
WSDL      :http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx?wsdl

Attached reference link

Common Webservice interface address

https://blog.csdn.net/qq_41694906/article/details/88029533

Topics: Java Back-end