webservice--using JDK development WebService

Source: Internet
Author: User
Tags webservice annotation wsdl

First, the development means of webservice

The following two development tools are available when developing WebService with Java

1. Using JDK development (1.6 and later)

2, the use of CXF Framework development (at work)

Second, 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:

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

 1  package me.gacl.ws; 2  3 import javax.jws.webmethod; 4 import  javax.jws.webservice; 5  6 /** 7  *  @author  gacl 8   *  Define SEI (Webservice endpoint interface (terminal))  9  */10 // Labeling WEBSERVICEI interfaces with @webservice annotations 11  @WebService12  public interface WebServiceI {13  14     //uses @webmethod annotations to annotate methods in the Webservicei interface 15     @ Webmethod16     string sayhello (String name);17      18      @WebMethod19      string save (string  NAME,STRING PWD); 20 } 

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

2, write the implementation class of interface, use @WebService annotation Annotation Implementation class, implement all the methods defined in the interface, as follows:

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

 1 package me.gacl.ws; 2  3 import javax.jws.WebService; 4   5 /** 6  *  @author  gacl 7  * sei-specific implementation  8   */ 9 //implementation class for WEBSERVICEI interfaces using @webservice annotation annotations webserviceimpl10  @WebService11  public  class webserviceimpl implements webservicei {12 13     @ Override14     public string sayhello (String name)  {15          system.out.println ("webservice sayhello " +name); 16          return  "sayhello " +name;17      }18 19      @Override20      public  String save (STRING NAME, STRING PWD)  {21          system.out.println ("Webservice save  "+name+",  "+pwd); 22         return   "save success"; 23     }24 }

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

3, use endpoint (terminal) class to publish WebService, the code is as follows:

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

 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  *10  *  Release web service11  */12  Public class webservicepublish {13 14     public static  void main (String[] args)  {15         // 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 publishing address is legal 17         //string address =   "Http://192.168.1.100:8989/Webservice"; this Webservice is the legal 18        to publish the address.   String address =  "http://192.168.1.100:8989/ws_server/webservice "; 19         // Use the Publish method provided by the endpoint class to publish the webservice, ensuring that the port number used is not occupied by other applications when publishing 20          endpoint.publish (Address , new webserviceimpl ());21          system.out.println ("Release WebService success!"); 22     }23 }

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

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:

650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291553270817392.png "style=" border:0px; " />

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

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

 1 package me.gacl.listener; 2  3 import  javax.servlet.servletcontextevent; 4 import javax.servlet.servletcontextlistener; 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 publishing WebService 12  */13 //using the @ Servlet3.0 provided The Weblistener annotation implements the Webservicepublishlistener class labeled Servletcontextlistener interface as a listener14  @WebListener15   Public class webservicepublishlistener implements servletcontextlistener {16 17       @Override18      public void contextdestroyed ( SERVLETCONTEXTEVENT SCE)  {19 20     }21 22       @Override23      public void contextInitialized (SERVLETCONTEXTEVENT SCE)  {24         // WebService's release address 25         string address =  "http:/ /192.168.1.100:8080/ws_server/webservice "; 26         // Publishing the Webservice,webserviceimpl class is a concrete implementation class for the Webservie interface 27          Endpoint.publish (Address , new webserviceimpl ());28          system.out.println ("Use Webservicepublishlistener to publish WebService success!"); 29     }30 }

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

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:

650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291648418945385.png "style=" border:0px; " />

2. Use servlet to publish WebService

650 "this.width=650;" src= "http:// Common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

 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   *  servlet12  */13 //for publishing WebService using the @ Servlet3.0 provided The Webservlet annotation will label the generic Java class that inherits the HttpServlet class as a servlet14 //to set the Value property to an empty string. This way the Webservicepublishservlet does not provide the path to the external Access 15 //loadonstartup property setting Webservicepublishservlet initialization Time 16 @ Webservlet (value= "", Loadonstartup=0) 17 public class webservicepublishservlet extends  httpservlet {18 19     /*  (Non-javadoc) 20       *  @see  javax.servlet.genericservlet#init () 21      *  In WebservicepublIshservlet released when initializing webservice22      */23     public  Void init ()  throws ServletException {24          //webservice's release address 25         string address =   "Http://192.168.1.100:8888/WebService"; 26         // Publishing the Webservice,webserviceimpl class is a concrete implementation class for the Webservie interface 27          Endpoint.publish (Address , new webserviceimpl ());28          system.out.println ("Use Webservicepublishservlet to publish WebService success!"); 29     }30 }

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

When you deploy a Web app to a server run, WebService is released when you initialize Webservicepublishservlet. As shown in the following:

650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291639417221127.png "width=" 950 "style=" border:1px solid #000000; "/>

We can then use the URL address of the publication to access WebService, as shown in:

650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291644548476288.png "style=" border:0px; " />

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:

650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291705220198829.png "style=" border:1px Solid #000000; "/>

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:

650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291708542226858.png "style=" border:1px Solid #000000; "/>

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:

650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291714174096228.png "style=" border:0px; " />

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:

650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291716493008897.png "style=" border:1px Solid #000000; "/>

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:

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

 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  *  calls to WebService clients  9  */10 public  Class wsclient {11 12     public static void main ( String[] args)  {13         // Create a factory for generating Webserviceimpl instances, the Webserviceimplservice class is generated by the Wsimport tool 14          webserviceimplservice factory = new webserviceimplservice ();15          //generates a Webserviceimpl instance from the factory, Webserviceimpl is generated by the Wsimport tool 16          WebServiceImpl wsImpl =  Factory.getwebserviceimplport (); 17         //calls the SayHello method of WebService 18         string resresult  = wsimpl.sayhello ("Aloof pale Wolf");19          System.out.println ("Call WebService's SayHello method returns the result:" +resresult);20          system.out.println ("---------------------------------------------------");21          //calls WebService's Save method 22          resresult = wsimpl.save ("Aloof Pale Wolf", "123");23          System.out.println ("Call WebService's Save method returns the result:" +resresult); 24     }25 }

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

The client calls the server-side WebService method to run the results as follows:

650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291732044728721.png "style=" border:1px Solid #000000; "/>

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

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.