Jdk6 features 11: simpler, more powerful JAX-WS

Source: Internet
Author: User
Jdk6 features 11: simpler, more powerful JAX-WS

History of JAX-WS2.0

--------------------------------------------------------------------------------
JAX-WS (JSR-224) is the abbreviation of Java architecture for XML Web Services, simply put, is a framework for developing web services applications with Java and XML, the current version is 2.0, it is a later version of JAX-RPC 1.1, J2EE 1.4 carries a JAX-RPC1.1, while Java ee 5 includes JAX-WS 2.0, but still supports JAX-RPC for backward compatibility. now sun has put the JAX-WS directly in Java SE 6, because the JAX-WS will use the common annotation (JSR 250), Java Web services metadata (JSR 181), jaxb2 (JSR 222 ), stax (JSR 173), So Sun must also put the last few components that are originally in the Java EE category to Java SE, now we can clearly understand why Sun wants to include the components that seem unrelated to Java SE. The ultimate goal is to support Web Services in Java SE.

Architecture of JAX-WS2.0

--------------------------------------------------------------------------------
JAX-WS is not an isolated framework, it relies on numerous other specifications, essentially it consists of the following parts
1. Java APIs used to develop Web Services
2. the XML binding mechanism is used to process marshal/unmarshal. The JAX-WS2.0 uses jaxb2 to process the ing between Java object and XML, unmarshalling maps XML to Java object. the reason why we need to map Java objects to XML is that the Java objects used as method parameters and return values must be transmitted through the network transmission protocol (generally soap, this requires that Java objects be serialized and deserialized in a way similar to XML in soap.
3. Numerous metadata (annotations) will be used by the JAX-WS to describe the related classes of web services, including common annotations, Web services metadata, jaxb2 metadata and JAX-WS2.0 specifications of their own metadata.
4. annotation processing tool (APT) is an important part of the JAX-WS, because the JAX-WS2.0 specification uses a lot of metadata, so apt is needed to deal with a lot of annotations. in <jdk_home>/bin, there are two Commands: wsgen and wsimport, which use apt and compiler APIs to process the annotations encountered, wsgen can generate and compile necessary help classes and related support files for Web Services Providers. wsimport uses WSDL as the input for Web service consumer to generate and compile necessary help classes and related support files.
5. The JAX-WS also includes the contractual relationship between the JAX-WS runtime and the application server and the tool

Programming Model of JAX-WS2.0

--------------------------------------------------------------------------------
Now it is very simple to write web services with JAX-WS2.0, unlike JAX-RPC, JAX-WS can expose any pojo as Web Services, the service class does not need to implement the interface, the service method does not have to throw RMI exception. the following describes how to use JAX-WS2.0 to develop and test Web Services in the jdk6 environment.
1. write a service class and mark it with Web services metadata (JSR-181). I use another new feature of blogjdk6 10: wsprovider class in Web Service metadata as an example of a service class, here I will repeat the source code of the wsprovider class:
/**
* @ Author chinajash
*/
@ WebService (targetnamespace = "http://blog.csdn.net/chinajash", servicename = "helloservice ")
Public class wsprovider {
@ Webresult (name = "greetings") // customize the description of the return value of this method in the WSDL
@ Webmethod
Public String sayhi (@ webparam (name = "myname") string name ){
Return "hi," + name; // @ webparam is the description of the custom parameter name in the WSDL.
}
@ Oneway // indicates that the service method is one-way. Neither the return value nor the declared check exception
@ Webmethod (Action = "printsystemtime", operationname = "printsystemtime") // customize the description of this method in WSDL
Public void printtime (){
System. Out. println (system. currenttimemillis ());
}
Public static void main (string [] ARGs ){
Thread wspublisher = new thread (New wspublisher ());
Wspublisher. Start ();
}
Private Static class wspublisher implements runnable {
Public void run (){
// Publish wsprovider to http: // localhost: 8888/chinajash/wsprovider. Before that, you must call the wsgen command.
// Generate the wsprovider Support class. The command is as follows:
// Wsgen-CP. WebServices. wsprovider
Endpoint. Publish ("http: // localhost: 8888/chinajash/wsprovider", new wsprovider ());
}
}
}
2. use wsgen to generate the necessary help classes for the above service class, and then call the static method publish of the endpoint class to publish the service class (refer to the new features of my blog jdk6 in Step 10: web Service metadata). Here I publish the service class to http: // localhost: 8888/chinajash/wsprovider.
3. Use wsimport to generate necessary helper classes for the service consumer (that is, the service client). The command is as follows:
Wsimport http: // localhost: 8888/chinajash/wsprovider? WSDL
This will generate the client help class under <Current directory>/NET/csdn/blog/chinajash. In this example, seven classes will be generated.
Helloservice. Class
Objectfactory. Class
Package-info.class
Printsystemtime. Class
Sayhi. Class
Sayhiresponse. Class
Wsprovider. Class
4. Use the following code on the client to call the Web service defined in step 1:
Helloservice HS = new helloservice ();
Wsprovider Ws = HS. getwsproviderport ();
System. Out. println (WS. sayhi ("chinajash "));
WS. printsystemtime ();
After the above Code is called, the client console outputs
Hi, chinajash
Server console output server current system time

 

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.