Based on the introduction of the WebService Learning Note Series (ii), using Tcpmon to capture the XML content we send or receive, we will make a simple analysis of these XML content in the next day. Remember in the WebService study Note series (iv) we described in detail the server-side program writing, the service-side program of this article still follow the service-side code, but the series (iv) on the interface of the annotations are not explained in detail, today first to see what the role of annotations on the interface.
@WebService public interface imyserver { @WebResult (name= "Addresult" ) public int add (@webparam (Name= "a" ) int a, @WebParam (name= "B" ) int b); public int minus (int a,int b);}
The above code is an interface that we define on the server side, and the class that implements the interface can be published for other people to invoke. There are two methods in this interface, one is add, the other is minus, the two methods are added @webresult (name= "Addresult") annotation on the Add method, the Add parameter was added @webparam (name= "a") Annotations, and no annotations are added to minus, let's look at how the annotated method differs from the one that didn't add the annotation.
publicclass MyFirstClient { publicstaticvoidmain(String[] args) { new MyServerImplService() .getMyServerImplPort(); System.out.println(myServerImpl.add(34)+"------"); myServerImpl.minus(71); }}
This is a client invocation of the service-side code, the specific invocation method is shown here. The client calls the server-side two methods, one is the Add method, the other is the minus method, and we take a look at the results of Tcpmon capture (see here for Tcpmon usage).
When the Add method is called, the XML that is sent
<?xml version= "1.0"?> <s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:body> <ns2:add xmlns:ns2="http://lenve.server/"> <a>3</a> <b>4</b> </ns2:add> </s:body> </s:envelope>
When you call add, the XML that is received:
<?xml version= "1.0"?> <s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:body> <ns2:addresponse xmlns:ns2="http://lenve.server/"> <addresult>7</addresult> </ns2:addresponse> </s:body> </s:envelope>
Call minus is the XML that is sent:
<?xml version= "1.0"?> <s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:body> <ns2:minus xmlns:ns2="http://lenve.server/"> <arg0>7</arg0> <arg1>1</arg1> </ns2:minus> </s:body> </s:envelope>
XML received when calling minus:
<?xml version= "1.0"? << Span class= "Hljs-title" >s:envelope xmlns:s = " http://schemas.xmlsoap.org/soap/envelope/"; < s:body ; <NS2: Minusresponse xmlns:ns2 = "HTTP +/ lenve.server/"; <return ; 6 Span class= "Hljs-tag" ></return ; </ns2:minusresponse ; </S: Body ; </s:envelope ;
The
Add and minus methods require two parameters, because we have specified the parameter name @webparam (name= "a") of the Add method in the interface of the server, so when the Add method is called, it is sent <a>3</a> <b>4</b>
, while calling minus, sends the <ARG0>7</ARG0><ARG1>1</ARG1>
, This is because we do not specify the parameter name, so the system assigns a parameter name, a good server must specify a meaningful parameter name, so as to facilitate third-party invocation, and for the return value is the same, if the return value name is specified @WebResult (name= " Addresult ")
, the XML returned by the system will use <addresult>7</addresult>
, if no return name is specified, the system will use the default return <return>6</return>
. This is the function of adding annotations to the service side. Namespaces can also be specified manually, <ns2:minus xmlns:ns2= "http://lenve.server/";
, if you do not want to use this namespace, You can specify the namespace name manually by using annotations on the service side by specifying the method:
@WebService (targetnamespace= "http://www.lenve.test" ) public interface IMyServer { @WebResult (name= "Addresult" ) public int add (@< Span class= "Hljs-title" >webparam (Name= "a" ) int a, @WebParam (name= "B" ) int b); public int minus (int a,int b);}
SOAP Message parsing