A true lightweight WebService framework--using JAX-WS (JWS) to publish WebService

Source: Internet
Author: User
Tags time and date wsdl

WebService has historically been highly valued, especially in the Java Camp, WebService framework and technology. Well-known xfile (new such as CXF), Axis1, Axis2 and so on.

and Sun is not far behind, from the early Jax-RPC to now mature, support RPC calls and messaging Jax-WS have been tested by the market, very mature, and using JAX-WS development WebService revenue is very large, it is lightweight.

  

The development of WebService using JAX-WS requires only a few simple steps: The write interface and the implementation = = Release and generate the client (test or use).

In the development phase, we do not need to import external jar packages, because these APIs are readily available. First, the interface is written (the interface only need to put the class as @webservice, the method to be exposed to the client as @webmethod can be, the rest, such as @webresult, @WebParam, etc. are not necessary, While the client and server communication with RPC and message-oriented two kinds, the difference and configuration later):

Package service;

Import Java.util.Date;
Import Javax.jws.WebMethod;
Import Javax.jws.WebParam;
Import Javax.jws.WebResult;
Import Javax.jws.WebService;
Import javax.jws.soap.SOAPBinding;

/**
* As a Test WebService interface
*
* @author johness
*
*/
@WebService
@SOAPBinding (style = SOAPBinding.Style.RPC)
Public interface Sayhiservice {
/**
* WebService method for performing tests
*/
@WebMethod
void Sayhidefault ();

/**
* WebService method for executing tests (with reference)
*
* @param name
*/
@WebMethod
void Sayhi (@WebParam (name = "name") String name);

/**
* WebService method for executing the test (for time check)
*
* @param clenttime Client time
* @return 0 indicates a time check failure 1 indicates a validation success
*/
@WebMethod
@WebResult (name = "valid")
int Checktime (@WebParam (name = "Clienttime") Date clienttime);
}

Then is the implementation class (annotated @webservice and its endpointinterface properties are necessary):

Package service.imp;

Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Javax.jws.WebService;
Import javax.jws.soap.SOAPBinding;
Import service. Sayhiservice;

/**
* The WebService implementation class as a test
*
* @author johness
*
*/
@WebService (endpointinterface = "service. Sayhiservice ")
@SOAPBinding (style = SOAPBinding.Style.RPC)
public class Sayhiserviceimp implements Sayhiservice {

@Override
public void Sayhidefault () {
System.out.println ("Hi, johness!");
}

@Override
public void Sayhi (String name) {
System.out.println ("Hi," + name + "!");
}

@Override
public int checktime (Date clienttime) {
Accurate to seconds
String dateserver = new Java.sql.Date (System.currenttimemillis ())
. ToString ()
+ " "
+ New Java.sql.Time (System.currenttimemillis ());
String dateclient = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss")
. Format (clienttime);
Return Dateserver.equals (dateclient)? 1:0;
}
}

Then there is the release (usually in two ways):

Mode one (this method can only be used as debugging, there are the following bugs:

Jdk1.6u17? The following compiler does not support SOAP that publishes document in Endpoint.publish manner, and must be added in the service interface and implementation class @SOAPBinding (style = SOAPBinding.Style.RPC) "annotations;

Access restricted, it seems that only native access (should be bound to the publish URL, such as under the use of localhost can only be native access) ...) :

Package Mian;

Import Javax.xml.ws.Endpoint;
Import SERVICE.IMP.SAYHISERVICEIMP;

public class Main {
/**
* Release WebService
* Simple
*/
public static void Main (string[] args) {
Endpoint.publish ("Http://localhost:8080/testjws/service/sayHi", New Sayhiserviceimp ());
}
}

Mode two (Web server-based servlet mode):

In Tomcat, for example, first write the Sun-jaxws.xml file and put it under Web-inf:

<?xml version= "1.0" encoding= "UTF-8"?>
<endpoints xmlns= "Http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version= "2.0" >
<endpoint name= "Sayhiservice"
implementation= "Service.imp.SayHiServiceImpl"
url-pattern= "/service/sayhi"/>
</endpoints>

Then change the Web. XML to add Listener and Servlets (Url-pattern to the same OH):

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns : web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "version=" 2.5 ">

<listener>
<listener-class>
Com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>SayHiService</servlet-name>
<servlet-class>
Com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SayHiService</servlet-name>
<url-pattern>/service/sayHi</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Finally deployed to Tomcat, it is worth mentioning that you may need to add the following jar packages (because Tomcat does not):

Start Tomcat.

Service-side work is done, pay attention to two things.

Note: The project needs to use UTF-8 encoding (at least sun-jaxws.xml must be in UTF-8 format);

For MyEclipse built-in Tomcat, there may be no need to manually add the above jar packages, but should be added when deploying independently because they use different class-path;

Multiple interfaces of different paths should also use the same wsservlet;

It is best to add @soapbinding (style = SOAPBinding.Style.RPC) annotations.

After deployment, open the browser input URL: http://localhost:8080/testjws/service/sayHi?wsdl. You can see things and prove that the release is successful.

Attach the project Tree chart:

Finally, the client uses, because WebService is platform-and language-independent, XML-based, so we can write or build clients in different languages entirely.

There are generally three ways to use (in the case of the Java language):

One, use the JDK's own tool Wsimport to generate the client:

With the Wsimport tool generated by the JDK, I generated the client file into the desktop src file (-D) and reserved the source file (-keep), specifying the package name (-P).

Then we can use the generated file to invoke the method that the server exposes:

It is worth mentioning that the JDK you build using and your client's JRE need to be matched!

From the above directory structure we can find: each WebMethod of the server is parsed into a single class (if the entity is used, the entity will be parsed to the client, and the source code, so it is recommended to use the entity carefully).

(The diagram above is an old diagram, just to show that JAXWS is generating classes for each WebMethod)

Our service is generated by a proxy class to invoke the services, and then we'll look at the usage:

Package test;

Import Java.util.Date;
Import Java.util.GregorianCalendar;
Import javax.xml.datatype.DatatypeConfigurationException;
Import Javax.xml.datatype.DatatypeFactory;
Import Javax.xml.datatype.XMLGregorianCalendar;
Import Testjws.client.SayHiService;
Import Testjws.client.SayHiServiceImpService;

public class Main {

public static void Main (string[] args) throws Datatypeconfigurationexception {
Get Service
Sayhiservice service = new Sayhiserviceimpservice (). Getsayhiserviceimpport ();

Sayhi
Service.sayhidefault ();
Service.sayhi ("Ahe");

Checktime
This is mainly about the time and date of the XML transfer, the method is also slightly complex
GregorianCalendar calender = new GregorianCalendar ();
Calender.settime (New Date (System.currenttimemillis ()));
Xmlgregoriancalendar xmldate = Datatypefactory.newinstance (). Newxmlgregoriancalendar (calender);
System.out.println (Service.checktime (xmldate));
}
}

Look at the output of the server, whether we call success:

It worked!

For the method of checking time the client also received feedback:

Second, use a project such as MyEclipse (Eclipse for Jave ee) to create a web Service client

Then fill in the WSDL address and I won't post the next steps.

Three, write their own code-_-, in fact, this method has to say is the best.

A true lightweight WebService framework--using JAX-WS (JWS) to publish WebService

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.