Java interface call -- webservice is just an RPC, webservicerpc

Source: Internet
Author: User
Tags webservice annotation

Java interface call -- webservice is just an RPC, webservicerpc

Many new users are forced to hear the interface and do not know what the interface is! In fact, the interface is RPC. By Remotely accessing the methods provided by other programs, you can obtain the interface for executing the method without executing the method locally. This is an upgraded version of local method calls. I will talk about how to implement rpc through socket tomorrow, as well as service registration and dynamic online/offline. Here, we will first discuss the RPC implementer as a webservice to facilitate later understanding of the source code execution process. The framework is to provide more convenient use based on the principle. The protocol is based on TCP or UDP, the service provider and the caller agree on the format in which the message is sent and parsed. The agreement is not profound.

Discuss with author: http://www.cnblogs.com/intsmaze/p/6055684.html

Supports website development and java development.

Sina Weibo: intsmaze Liu Yang Ge

: Intsmaze

The following are my notes when I learned webservice many years ago. I will sort out and share them today to prepare for RPC Analysis of my source code.

WebService, as its name implies, is a Web-based service. It uses the Web (HTTP) method to receive and respond to a request from an external system, thus achieving remote calls. We can call the Web Service for querying weather information on the Internet and embed it into our program (C/S or B/S program, the user can see the weather information at our outlets. He will think that we provide him with a lot of information services, but in fact, we simply call a piece of code on the server to call the WebService written by others. WebService can publish your service (a piece of code) to the Internet for others to call, or call the WebService published on another machine, just like using your own code.

Webservice is a remote call between two software systems. The call here is a cross-language call. Two different applications exchange data through xml. In this way, data in xml files can be parsed in any language. The Protocol for data interaction is http. The http protocol and the underlying layer for accessing the database using jdbc depend on socket connections. We can also log on to a third-party account on other websites more often than I do. In fact, we also use webservice. Other websites get accounts to third-party account services for verification.

After JDK1.6, The JAX-WS Specification defines how to publish a webService.
JAX-WS refers to Java Api for XML-WebService.
Use JDK and later versions to publish a WebService.
Classes related to Web services are all located in the javax. jws. * package.
Main categories include:
@ WebService-it is an annotation that marks Java classes as Web services or Java interfaces as Web Service interfaces.
Endpoint-This class is an Endpoint service class. Its publish method is used to bind a @ WebService annotation object that has been added to an address port and receive two parameters, one being a local service address, the second is the class that provides services.
How to publish a web Service:
1. Add the @ WebService annotation to the class.
2. Use EndPoint (Endpoint Service) static EndPoint. publish (String address, Object implementor) to publish a webService.
After the EndPoint service is published, an independent thread will be started to run. This starting thread is actually a servicesocket, which will receive socket connections from other clients.
Other considerations:
1. After @ WebService annotation is added to the class, all non-static methods in the class will be published. Static methods and final methods are not supported.
2. If you want a method (non-static, non-final) not to be made public, you can add @ WebMethod (exclude = true) to the method to prevent the method from being made public.
3. If the @ WebService annotation is added to a class, at least one public method must be provided for this class; otherwise, the startup will fail.

@ WebServicepublic class HelloService {public String sayHello (String intsmaze) {System. out. println ("sayHello ()... "); return" hello "+ intsmaze;} public String sayHello2 (String intsmaze) {return" hello "+ intsmaze;} public static void main (String [] args) {Endpoint. publish ("http: // 127.0.0.1: 6789/hello", new HelloService ());
// This part is actually encapsulated. It starts a servicesocket Based on the specified parameters and generates a WSDL document. System. out. println ("Server ready ...");}}

How can I call the service after it is released successfully? Please refer to the instruction manual-WSDL:
In the address bar of any service, enter the service address plus? Wsdl: http: // 127.0.0.1: 6789/hello? Wsdl
Currently, instead of accessing webService, you only need to obtain a Description file used to describe WebService, that is, the wsdl file. wsdl-WebService Description Language, which describes WebService instructions in XML format ",With the instructions, we can know how to use or call this service.

Wsimport.exe comes with jdk and can generate client call code according to the wsdl document.
Of course, no matter what language the WebService on the server uses, Java code will be generated on the client. What is written on the server is not important.
Note that the generated code is not downloaded from the server.
Wsimport.exe is located in the JAVA_HOME \ bin directory.
Common parameters:
-D <directory>-The. class file will be generated. Default parameter.
-S <directory>-The. java file will be generated.
-P <new package name generated>-place the generated class under the specified package:-p com. intsmaze. demo
(Wsdlurl)-http: // server: port/service? Required parameter.
Example:
C:/> wsimport-s. http: // 192.168.0.100/one? Wsdl
Note:-s cannot be separated.-s is followed by a small dot to specify the directory generated by the source code. Point is the current directory. (Note: There are spaces before and after)
If the-s parameter is used, two codes are generated in the directory, one of which is. class code. One copy is. java code.
. Class code, which can be used after packaging. Java code can be directly copied to our project for running.
Then, you only need to call the method provided by the generation class based on the information provided by the wsdl file. We recommend that you look up from the bottom.

  Wsimport.exe comes with jdk. You can generate a client to call java code based on the wsdl document. Of course, if you use a tool similar to other languages, the code in the corresponding language will be generated after the wsdl is parsed, here we only use java as an example. Note that these codes are not downloaded from the server, but are parsed to generate the corresponding java file (that is, a local IO) by parsing the wsdl ).
Wsimport.exe is located in the JAVA_HOME \ bin directory.

The common parameter is-d <directory>-to generate the. class file. Default parameter. -S <directory>-The. java file will be generated. -P <new package name generated>-place the generated class under the specified package:-p com. intsmaze. demo (wsdlurl)-http: // server: port/service? Required parameter.

Example: C:/> wsimport-s. http: // 192.168.0.100/one? Wsdl
Note:-s cannot be separated.-s is followed by a small dot to specify the directory generated by the source code. Point is the current directory. (Note: There are spaces before and after)
If the-s parameter is used, two codes are generated in the directory, one of which is. class code. One copy is. java code .. Class code, which can be used after packaging. Java code can be directly copied to our project for running.

Generate code

Then, you only need to call the method provided by the generation class based on the information provided by the wsdl file. We recommend that you look up from the bottom.

Wsdl: definitions xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: wsdl = "http://schemas.xmlsoap.org/wsdl/" xmlns: tns = "http://jdkservice.intsmaze.com/" xmlns: soap = "http://schemas.xmlsoap.org/wsdl/soap/" xmlns: ns1 = "http://schemas.xmlsoap.org/soap/http" name = "HelloServiceService" targetNamespace = "http://jdkservice.intsmaze.com/">This is the package structure of the server. In general, the most comments are modified. Do not expose them!<Wsdl: types> <xs: schema xmlns: xs = "http://www.w3.org/2001/XMLSchema" xmlns: tns = "http://jdkservice.intsmaze.com /"
ElementFormDefault = "unqualified" targetNamespace = "http://jdkservice.intsmaze.com/" version = "1.0"> <xs: element name = "sayHello" type = "tns: sayHello"/> <xs: element name = "sayHelloResponse" type = "tns: sayHelloResponse"/> <xs: complexType name = "sayHello"> <xs: sequence> <xs: element minOccurs = "0" name = "arg0" type = "xs: string"/> <xs: element name = "arg1" type = "xs: int "/> </xs: sequence> </xs: complexType> <xs: complexType name =" sayHelloResponse ">6. The child element describes its type, whether it is a parameter or a return value.<Xs: sequence> <xs: element minOccurs = "0" name = "return" type = "xs: string"/> </xs: sequence> </xs: complexType> </xs: schema> </wsdl: types> <wsdl: message name = "sayHelloResponse">5. You can know the parameter type through element.<Wsdl: part element = "tns: sayHelloResponse" name = "parameters"> </wsdl: part> </wsdl: message> <wsdl: message name = "sayHello"> <wsdl: part element = "tns: sayHello" name = "parameters"> </wsdl: part> </wsdl: message> <wsdl: portType name = "HelloService">3. Find the sub-element of the tag, which is the provided method.<Wsdl: operation name = "sayHello">Method Name<Wsdl: input message = "tns: sayHello" name = "sayHello"> </wsdl: input>
4. input parameters. you can know the parameter type through the message attribute. However, if you generate local code, you can know the parameter type by calling the function.<Wsdl: output message = "tns: sayHelloResponse" name = "sayHelloResponse"> output parameters </wsdl: output> </wsdl: operation> </wsdl: portType> <wsdl: binding name = "HelloServiceServiceSoapBinding" type = "tns: HelloService">2. Find the corresponding tag Based on the type attribute.<Soap: binding style = "document" transport = "http://schemas.xmlsoap.org/soap/http"/> <wsdl: operation name = "sayHello"> <soap: operation soapAction = "" style = "document"/> <wsdl: input name = "sayHello"> <soap: body use = "literal"/> </wsdl: input> <wsdl: output name = "sayHelloResponse"> <soap: body use = "literal"/> </wsdl: output> </wsdl: operation> </wsdl: binding> <wsdl: service name = "HelloServiceService">The name of the Service to create a specific service object.<Wsdl: port binding = "tns: HelloServiceServiceSoapBinding"1. Find the corresponding tag name based on the name.= "HelloServicePort">The service object calls getHelloServicePort () to obtain the port and return the service interface.<Soap: address location = "http: // 127.0.0.1: 7777/hello"/> </wsdl: port> </wsdl: service> </wsdl: definitions>

 

Package com. intsmaze. jdkservice;/*** use wsimport to generate client code to call webservice */public class App {public static void main (String [] args) {/*** wsdl: <service name = "HelloServiceService"> */HelloServiceService hss = new HelloServiceService ();/*** wsdl: <port name = "HelloServicePort" binding = "tns: helloServicePortBinding "> */HelloService soap = hss. getHelloServicePort (); String str = soap. sayHello ("intsmaze ");// Here we can see that we are calling our local method. In fact, we internally assemble the sent data into the soap protocol,
Then
The data is sent to the server, and the server thread receives the request to process the returned data.
System. out. println (str );}}

We have used the HTTP call process obtained by HttpWatch and obtained detailed information about the HTTP request header and other requests. Since WebServie communicates over HTTP, can we not use HTTPWatch to obtain its request process? Our code is not only the HTTP protocol sent to the server, but also the SOAP protocol, which is the basis for WebService communication. To obtain the format of sending and receiving SOAP data. We need to use a tool to gain a deep understanding of WebService.
We use TCP/IP Monitor to Monitor the complete process of intercepting requests and responding to specific data.

The following HTTP requests are sent:

The response information is the same as the sending information. It must first be HTTP and then follow the SOAP protocol.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.