First, the use of JDK development WebService2.1, development WebService server side
1, define a interface, use @WebService Annotation Callout interface, use @WebMethod annotations to label all methods defined in the interface, as follows:
1 package me.gacl.ws; 2 3 import javax.jws.WebMethod; 4 import javax.jws.WebService; 5 6/** 7 * @author GaCl 8 * Define SEI (Webservi Ce EndPoint Interface (terminal)) 9 */10//Use @webservice annotation Callout WEBSERVICEI Interface One @WebService12 public Interface webservicei {13 + //Use @webmethod annotations to annotate methods in the Webservicei interface @WebMethod16 string SayHello (string name); 18 @WebMethod19 String Save (string name,string pwd); 20}
2, write the implementation class of interface, use @WebService annotation Annotation Implementation class, implement all the methods defined in the interface, as follows:
1 package me.gacl.ws; 2 3 import javax.jws.WebService; 4 5/** 6 * @author gacl 7 * Specific implementation of SEI 8 */ 9//Use @webservice annotation callout Webse Rvicei interface Implementation Class WEBSERVICEIMPL10 @WebService11 public class Webserviceimpl implements WEBSERVICEI { @Override14 Public string SayHello (string name) { System.out.println ("WebService SayHello" +name); SayHello "+name;17 }18 @Override20 public string Save (string name, string pwd) { System.out.println ("WebService Save" +name+ "," +pwd); " save Success"; }24}
3, use endpoint (terminal) class to publish WebService, the code is as follows:
1 package me.gacl.ws.test; 2 3 import javax.xml.ws.Endpoint; 4 5 import Me.gacl.ws.WebServiceImpl; 6 7/** 8 * @author GaCl 9 * 0 * Publish Web Service11 */12 public class Webservicepublish {$ public static void Main (string[] args) { Define the WebService's publishing address, which is the URL address provided to the outside world to access Webervice, with the URL address format: HTTP://IP: Port number/xxxx16 //string address = "/http" 192.168.1.100:8989/"; This Webservice the address of the publication is legal //string address =" Http://192.168.1.100:8989/Webservice "; This Webservice is issued with the legal address = "Http://192.168.1.100:8989/WS_Server/Webservice"; Use the Publish method provided by the Endpoint class to publish the webservice, to ensure that the port number used when publishing is not occupied by another application endpoint.publish (address, new Webserviceimpl ( ); System.out.println ("Release WebService success!"); }23}
Run the Webservicepublish class, you can write a good Webservice published, Webservice access URL is: Http://192.168.1.100:8989/WS_Server/Webservice, As shown in the following:
Here we write a Webservicepublish class to publish the webservice, if it is a Web project, then we can use a listener or servlet to publish the WebService, as follows:
1. Use Servletcontextlistener Listener to publish WebService
1 package Me.gacl.listener; 2 3 import javax.servlet.ServletContextEvent; 4 import Javax.servlet.ServletContextLis Tener; 5 Import Javax.servlet.annotation.WebListener; 6 Import Javax.xml.ws.Endpoint; 7 Import Me.gacl.ws.WebServiceImpl; 8 9/**10 * @author GACL11 * Listener for release WebService 12 */13//using Servlet3.0 provided @weblistener annotations will implement the Servletcontextlistener interface of the Web The Servicepublishlistener class is labeled as a Listener14 @WebListener15 public class Webservicepublishlistener implements Servletcontextlistener {@Override18 public void contextdestroyed (Servletcontextevent sce) {19 20}21 22 Publication address of @Override23 public void contextinitialized (Servletcontextevent sce) {//webservice Address = "Http://192.168.1.100:8080/WS_Server/WebService"; 26//Release Webservice,webserviceimpl class is the specific implementation class of the Webservie interface 27 Endpoint.publish (Address, New Webserviceimpl ()); System.out.println ("Use Webservicepublishlistener publish Webse Rvice success! "); }30}
When a web app is deployed to the server runtime, WebService is published when the Web app context is initialized.
We can then use the URL address of the publication to access WebService, as shown in:
2. Use servlet to publish WebService
1 package Me.gacl.web.controller; 2 3 Import javax.servlet.ServletException; 4 Import Javax.servlet.annotation.WebServlet; 5 Import Javax.servlet.http.HttpServlet; 6 Import Javax.xml.ws.Endpoint; 7 Import Me.gacl.ws.WebServiceImpl; 8 9/**10 * @author GACL11 * for publishing WebService Servlet12 */13//Servlet3.0 annotations provided with @webservlet will inherit the normal Java class of the HttpServlet class as an S ERVLET14//Set the Value property to an empty string so that Webservicepublishservlet does not provide a path to the external access 15// Loadonstartup property setting Webservicepublishservlet initialization time @WebServlet (value= "", loadonstartup=0) public class Webservicepublishservlet extends HttpServlet {* * * (Non-javadoc) @see Javax.servlet.genericservlet#init () 21 * Published WebService22 */23 public void init () throws servletexception {24 at Webservicepublishservlet initialization WebService address of the publication of String address = "Http://192.168.1.100:8888/WebService"; 26//Release WEBSERVICE,WEBSERVICEI The MPL class is the specific implementation class of the Webservie interface Endpoint.publish (address, New Webserviceimpl ()); System.out.prIntln ("Use Webservicepublishservlet to publish WebService success!"); 29}30}
When you deploy a Web app to a server run, WebService is released when you initialize Webservicepublishservlet. As shown in the following:
We can then use the URL address of the publication to access WebService, as shown in:
About the release of WebService is mainly through the Javax.xml.ws.Endpoint class provided by the static method publish for publishing, if it is a common Java project, you can write a class specifically for publishing webservice, if it is a Web project, then you can use the servlet Contextlistener or servlet for publishing.
2.2. Develop WebService Client
1. Generate the client code with the Wsimort.exe tool of the JDK, the Wsimort.exe tool is located in the bin directory of the JDK, as shown in:
execute command: wsimport-keep URL (URL is the path to the WSDL file) to generate the client code.
Create a WebService client test project as shown in:
Open a command-line window, switch to the SRC directory, and execute "wsimport-keep http://192.168.1.100:8888/WebService?wsdl" to generate the client code, as shown in:
No error occurred during the execution of the command, the code was generated successfully, and the SRC directory was refreshed to see the generated code, as shown in:
2. Writing calls with generated code webservice methods available externally
The Wsimport tool has helped us generate several Java classes, but we only need to be concerned with the use of the Webserviceimplservice class and Webserviceimpl interfaces, as follows:
1 package me.gacl.ws.client; 2 3 import Me.gacl.ws.WebServiceImpl; 4 import me.gacl.ws.WebServiceImplService; 5 6/** 7 * @author GaCl 8
* Call WebService Client 9 */10 public class WSClient {one-to-all public static void Main (string[] args) { //Create a Raw Webserviceimpl instances of the factory, Webserviceimplservice class is Wsimport tool generated by the webserviceimplservice factory = new Webserviceimplservice (); //Generate a Webserviceimpl instance from the factory, Webserviceimpl is generated by the Wsimport tool Webserviceimpl Wsimpl = Factory.getwebserviceimplport (); + //Call WebService SayHello method, String Resresult = Wsimpl.sayhello ("Aloof pale Wolf"); System.out.println ("The SayHello method that calls WebService returns the result:" +resresult); System.out.println ("---------------------------------------------------");/ /Call WebService's Save method 22 Resresult = Wsimpl.save ("Aloof and pale Wolf", "123"); System.out.println ("Call WebService's Save method returns the result:" +resresult); }25}
The client calls the server-side WebService method to run the results as follows:
The results returned from the call show that the client code generated with the Wsimport tool has been successfully called to the method in WebService. The above is the use of JDK development WebService content.
webservice-using JDK development WebService