WebService tutorial details (2)

  • 2020-05-07 19:36:41
  • OfStack

In the last article, I introduced you to the WebService tutorial.

Why USES tools:

1. Use tools to better understand the process of WebService request

2. The format of SOAP data sending and receiving can be obtained by using WsExplore

3. The tool Tcp/Ip Monitor can monitor the specific data of the interceptor request header and response header

What is ?

SOAP is a text protocol based on XML encoding specification. Simply speaking, SOAP is the transmission of XML data on the basis of HTTP to achieve remote invocation (no matter what language your server is written in, as long as you receive XML data of SOAP protocol and return XML data of SOAP protocol, it can be invoked by any language).

Using an WsExplorer instance: verify that qq is online

When qqCheckOnLine validation in qqOnlineWebServiceSoap is used, it returns

qqCheckOnlineResponse
qqCheckOnlineResult (string): N

Click source to see the details as follows:

1: this is the outgoing message format:


http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://WebXml.com.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-
-
870931520

2: the following is the XML format received


http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-
- http://WebXml.com.cn/">
N

When qqOnlineWebServiceHttpGet or qqCheckOnline in qqOnlineWebServiceHttpPost are validated, both are returned

Nhttp://WebXml.com.cn/" > N >

Using Tcp/Ip Monitor

TCP/IP Monitor not only sees the SOAP data, but also gets the header information requested and received by HTTP.

1. Location: this tool is located at: window > show view > other > MyEclipse Common(common tools) > TCP/IP Monitor

2. This tool, which is equivalent to an agent, will listen to a local port after it is started, and then forward the request to the specified target IP and port.
After obtaining the data, the data will be returned intact to the customer. From the customer's point of view, this agent should always be the first to be accessed, otherwise we will not see the process of data transfer.

3. Configuration options:

On the open TCP/IP Monitor interface: view Menu > Properties > Add(added on the right)

Set to the following properties:

step 1:

1) local monitoring port(listen to local port number) :9876, randomly set 1 4-bit port number, 1 will be through the form of http://127.0.0.1:9876 access

2) host name (server to be listened to, e.g. www.ofstack.com) :

3) Port (listen to the target server's port) : 6666 - because we released for http WebService: / / 127.0.0.1:6666 / helloworld so, 6666 is the need to monitor the port number.

4) Type(type of listening) :
- TCP/IP: 1 will use the original address to access the request, such as user input: http: / / 127.0.0.1:9876 / helloworld & # 63; wsdl returns the same wsdl service access address as before.

-- HTTP: the destination address is used to proceed to the next request. Such as user input: http: / / 127.0.0.1:9876 / helloworld & # 63; wsdl when request method will use http: / / 127.0.0.1:6666 / helloworld to visit sayHi method. This method will no longer be represented. Because it's not the port number you're listening to.

In the monitor type, I chose TCP/IP, then through input in the address bar, http: / / 127.0.0.1:9876 / helloworld & # 63; wsdl, in the returned wsdl file to see: address changes.

time out: sets the connection time for unsuccessful access to 0.

Once set, click OK and then Start on the right to start the monitoring.

step 2:

On MyEclipse WebService configuration WSDL URL: http: / / 127.0.0.1:9876 / helloworld & # 63; wsdl, note that MyEclipse TCP/IP Monitor port is used. Rather than directly to visit our release http: / / 127.0.0.1:6666 / helloworld & # 63; wsdl

How does modify the contents of the wsdl file?

Use WebService's annotations.

1. @WebService - define the service

2. @WebMethod - define the method

3. @WebResult - define the return value

4. @WebParam - define parameters

Note: for annotations, different versions have different levels of support:

1. 1.5 not supported.

2. Full annotations must be used before 1.6.0_20.

3. After 1.6.0_21, you can annotate the class with just @WebService.

What the annotation does:

The WebService annotations provide a more visual description of the Web service. To generate the WSDL document.

When the WebService annotations are modified, the code generated by the client is also affected.

The method and parameter names of the calls have also changed.

Example:


@WebService(name="myName",// The corresponding portType name="myName" 
portName="myPort", // In the corresponding service port name="myPort" 
serviceName="myService",// The corresponding service name="myService" 
targetNamespace="http://leaf.com/mynamespace")// You can write it as you like java In the package 
public class HelloWorld{ 
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
@WebMethod(action="myAction",// define 1 a soapAction="myAction" Used to find this method to execute  
operationName="myOperationName")// Define the methods that can be called, which will generate the specific methods of the corresponding class, operation name=".." 
public @WebResult(name="mySayHelloResult")String// Define the name of the return value  
sayHello(){ 
return "HelloWorld"; 
} 
@WebMethod(action="mySayHiAction",operationName="mySayHiOperationName") 
public @WebResult(name="mySayHiResult")String sayHi(@WebParam(name="myParaName", 
// Put the parameter in the header to protect the parameter body In the  
header=true, 
mode=Mode.IN) 
String name){ 
String str = " Hello: "+name+" , the current time is: "+sdf.format(new Date()); 
return str; 
} 
public static void main(String[] args) { 
Endpoint.publish("http://127.0.0.1:6666/helloworld",new HelloWorld()); 
} 
}

3: after publishing the above program, we accessed it through MyEclipse's WebService Explorer

You'll find a different message than before, but it's still the same method that was called.

4: use wsimport again � s. http: / / 127.0.0.1:6666 / helloworld & # 63; wsdl generates java code and then calls it
Here is the calling code (it can be described as unrecognizable, but it does the same thing).


package com.leaf.mynamespace; 
public class Main { 
public static void main(String[] args) { 
// Through the analysis of wsdl Known from myService In the call getMyPort return myName 
MyName myName = new MyService().getMyPort(); 
// through myName the mySayHiOperationName To invoke the sayHi methods  
String str = myName.mySayHiOperationName(" wang "); 
System.err.println(str); 
} 
}

WebService tutorial details (2) to give you the introduction here, I hope to help you!


Related articles: