A true lightweight WebService framework--using JAX-WS (JWS) to publish WebService
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):
1 package service; 2 3 import java.util.Date; 4 5 import javax.jws.WebMethod; 6 import Javax.jws.WebParam; 7 Import Javax.jws.WebRes Ult 8 Import Javax.jws.WebService; 9 Import javax.jws.soap.soapbinding;10/**12 * as Test WebService interface * * @author Johness15 * 16 */17 @WebService18 @SOAPBinding (style = SOAPBinding.Style.RPC) public interface Sayhiservice { /**22 * The WebService method for performing the test is */24 @WebMethod25 void Sayhidefault (); /**28 * The WebService method for executing the test (with reference) is * * @param name31 */32 @WebMethod33 void Sayhi (@WebParam (name = "Name") String name); /**36 * WebService method for performing tests (for TIME check) PNS * @param clenttime Client Time 39 * @return 0 indicates that the time check failed 1 means that the checksum is successful , */41 @WebMethod42 @WebResult (name = "valid") int Checktime (@WebParam (name = "Clienttime") Date clienttime); 44}
Then is the implementation class (annotated @webservice and its endpointinterface properties are necessary):
1 package service.imp; 2 3 Import Java.text.SimpleDateFormat; 4 Import java.util.Date; 5 6 Import Javax.jws.WebService; 7 Import javax.jws.soap.SOAPBinding; 8 9 Import Service. SAYHISERVICE;10 11/**12 * As test WebService implementation class * @author JOHNESS15 * */17 @WebService (endpointinterface = "s Ervice. Sayhiservice ") @SOAPBinding (style = SOAPBinding.Style.RPC) public class Sayhiserviceimp implements Sayhiservice {20 @Override22 public void Sayhidefault () {System.out.println ("Hi, johness!"); }25 @Override27 public void Sayhi (String name) {System.out.println ("Hi," + name + "!"); }30 @Override32 public int checktime (Date clienttime) {33//accurate to seconds, String dateserver = New Java.sql.Date (System.currenttimemillis ()) + toString () + "" "Notoginseng + NE W Java.sql.Time (System.currenttimemillis ()); String dateclient = new SimpleDateFormat ("Yyyy-mm-dd hh:mm: ss "). Format (clienttime); dateserver.equals return (dateclient)? 1:0;41}42 43}
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) ...) :
1 package mian; 2 3 import javax.xml.ws.Endpoint; 4 5 import service.imp.SayHiServiceImp; 6 7 public class Main {8 9
/**10 * Released WebService11 * Simple */13 public static void Main (string[] args) { endpoint.publish ("Http://localhost:8080/testjws/service/sayHi", New Sayhiserviceimp ()); }16 17}
Mode two (Web server-based servlet mode):
In Tomcat, for example, first write the Sun-jaxws.xml file and put it under Web-inf:
1 <?xml version= "1.0" encoding= "UTF-8"? >2 <endpoints xmlns= "Http://java.sun.com/xml/ns/jax-ws/ri/runtime" 3 version= "2.0" >4 <endpoint name= "Sayhiservice" 5 implementation= "Service.imp.SayHiServiceImpl" 6 url-pattern= "/service/sayhi"/>7 </endpoints>
Then change the Web. XML to add Listener and Servlets (Url-pattern to the same OH):
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <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 "> 3 4 <listener> 5 <listener-class> 6 Com.sun.xml.ws.transport.http.serv Let. Wsservletcontextlistener 7 </listener-class> 8 </listener> 9 <servlet>10 <s Ervlet-name>sayhiservice</servlet-name> <servlet-class>12 Com.sun.xml.ws.transport. Http.servlet.WSServlet </servlet-class>14 </servlet> <servlet-mapping> 16 <servlet-name>SayHiService</servlet-name> <url-pattern>/service/sayHi</url-pattern> </servlet-mapping>19 <wElcome-file-list>21 <welcome-file>index.html</welcome-file>22 <welcome-file>index.htm </welcome-file>23 <welcome-file>index.jsp</welcome-file>24 </welcome-file-list>25 < /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:
1 package test; 2 3 Import java.util.Date; 4 Import Java.util.GregorianCalendar; 5 6 Import Javax.xml.datatype.DatatypeConfigurationException; 7 Import Javax.xml.datatype.DatatypeFactory; 8 Import Javax.xml.datatype.XMLGregorianCalendar; 9 Import testjws.client.sayhiservice;11 Import testjws.client.sayhiserviceimpservice;12 public class Main {14 15 public static void Main (string[] args) throws Datatypeconfigurationexception {16//Get Service17 Sayhiserv Ice Service = new Sayhiserviceimpservice (). Getsayhiserviceimpport ();//Sayhi20 Service.sayhi Default (); Service.sayhi ("Ahe"); 25///CHECKTIME24//The main point here is that the time-date XML is passed, and the method is slightly more complex. GregorianCalendar calender = new GregorianCalendar (); Calender.settime (New Date (System.currenttimemillis ())) ; Xmlgregoriancalendar xmldate = Datatypefactory.newinstance (). Newxmlgregoriancalendar (calender); Syst Em.out.println (SERVICE.CHECKtime (Xmldate)); 29 30}31 32}
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.
Welcome to our Communication group, when we are bored together to pass the time:
or through QQ to contact me:
(Last modified on 2014-03-08 00:23:18)
A true lightweight WebService framework--using JAX-WS (JWS) to publish WebService