WebService's JDK development WebService

Source: Internet
Author: User
Tags webservice annotation wsdl

WebService's JDK development WebService

First, Development tools and environment

1. jdk1.6 version above

2. eclipse4.5 version

Second, JDK development webservice

1, define a interface, use @WebService Annotation Callout interface, use @WebMethod annotations to label all methods defined in the interface, as follows:

Package com.me.ws;

Import Javax.jws.WebMethod;
Import Javax.jws.WebService;
/**
*
* @author Administrator
* Use @webservice annotations to annotate interfaces as a WebService service
*/

@WebService
Public interface Webserviceinterface {
/*
* Labeling methods in the Webserviceinterface interface using @webmethod annotations
*/
  @WebMethod
public string SayHello (string name);
  @WebMethod
public string Save (string name);
}

2. Define an implementation class to implement all the methods in the interface, and annotate the implementation class with @webservice annotations into a WebService service as follows:

Package com.me.ws;

Import Javax.jws.WebService;
/**
*
* @author Administrator
* Use @webservice annotation callout implementation class to implement class annotations as a WebService service
*/
//
@WebService
public class Webserviceimpl implements Webserviceinterface {

@Override
public string SayHello (string name) {
System.out.println ("Hello World" +name);
Return "Hello World" +name;
}

@Override
public string Save (string name) {
System.out.println ("Save" +name);
Return "Save" +name;
}

}

3. Publishing Service

3.1. Use the endpoint (terminal) class to publish the WebService as follows:

Package com.me.ws;

Import Javax.xml.ws.Endpoint;

/**
*
* @author Administrator
* Release of WebService Service (JDK is published using endpoint (terminal) class WebService service)
*/
public class Webservicepublish {

public static void Main (string[] args) {
/**
* 1, first provide a URL for others to visit the address
*/
String address = "Http://192.168.1.72:8089/ws_service/hello";
2, the use of endpoint publish method to publish this service
Endpoint.publish (Address, interface implementation Class)
Endpoint.publish (Address, New Webserviceimpl ());
System.out.println ("Release WebService success!");
}

}

After you run the Webservicepublish class, you can publish the well-written webservice and its WSDL access address is http://192.168.1.72:8089/ws_service/hello?wsdl.

The above is the Java Project WebService release, if it is a Web project, then we can use the listener or servlet to publish the WebService (it is important to note here that the servlet package referenced in the project must be 3.0 and later),

3.2, use the listener to publish WebService, the code is as follows:

Package com.me.ws; Import javax.servlet.ServletContextEvent; Import Javax.servlet.ServletContextListener; Import Javax.servlet.annotation.WebListener; Import Javax.xml.ws.Endpoint;  Import Me.gacl.ws.WebServiceImpl; /** * @author GaCl * Listener for release WebService *//UseThe @weblistener annotations provided by Servlet3.0 will implement the Webservicepublishlistener class of the Servletcontextlistener interface as a listener@WebListenerpublic class Webservicepublishlistener implements Servletcontextlistener {@Override public void contextdestroyed (Servletcontextevent SCE) {} @Override public void contextinitialized (Servletcontextevent sce) {//webservice's publishing address St         Ring address = "Http://192.168.1.72:8089/ws_service/hello";         The release Webservice,webserviceimpl class is the specific implementation class Endpoint.publish for the Webservie interface (address, new Webserviceimpl ());     System.out.println ("Use Webservicepublishlistener to publish WebService success!"); } }
To configure on Web. XML:

<?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 "id=" webapp_id "version=" 2.5 ">

<display-name>ws_service</display-name>
<!--load the custom listener--
<listener>
<listener-class> com.me.ws.webservicepublishlinster</listener-class>
</listener>
</web-app>

When a web app is deployed to the server runtime, WebService is published when the Web app context is initialized.
Then we can use the URL address of the publication to access WebService

3.3, use the servlet to publish WebService, the code is as follows:

Packagecom.me.ws; Import javax.servlet.ServletException; Import Javax.servlet.annotation.WebServlet; Import Javax.servlet.http.HttpServlet; Import Javax.xml.ws.Endpoint;  Import Me.gacl.ws.WebServiceImpl; /** * @author GaCl * The servlet used to publish the WebService *///Use Servlet3.0 provided @webservlet annotations to label the generic Java class that inherits the HttpServlet class as a servlet//will VA The Lue property is set to an empty string so that Webservicepublishservlet does not provide a path to the external Access//loadonstartup property setting Webservicepublishservlet initialization Time @WebServlet (value= "", loadonstartup=0) public class Webservicepublishservlet extends HttpServlet {/* (non-javadoc) * @see J Avax.servlet.genericservlet#init () * Released at Webservicepublishservlet initialization WebService */public void init () throws         servletexception {//webservice Address of the publication String = "Http://192.168.1.72:8089/ws_service/hello";         The release Webservice,webserviceimpl class is the specific implementation class Endpoint.publish for the Webservie interface (address, new Webserviceimpl ());     System.out.println ("Use Webservicepublishservlet to publish WebService success!"); } }

to configure on Web. XML:

<?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 "id=" webapp_id "version=" 2.5 ">
<display-name>ws_service</display-name>
<servlet>
<servlet-name>WSServlet</servlet-name>
<servlet-class>com.me.ws.WebServicePublishServlet</servlet-class>
<load-on-startup>1</load-on-startup> <!--must have--
</servlet>
<servlet-mapping>
<servlet-name>WSServlet</servlet-name>
<url-pattern>/ws_service</url-pattern>
</servlet-mapping>
</web-app>

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

  

Then we can use the URL address of the publication to access WebService
The above is the development of the WebService service, the following is about its client
Third, the development of WebService client

1. Generate the client code with the Wsimport.exe tool of the JDK, the Wsimport.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.72:8089/ws_service/hello?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's 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.