WebService Learning Summary (iii)--using JDK to develop webservice

Source: Internet
Author: User
Tags webservice annotation wsdl

WebService Learning Summary (iii)--using JDK to develop webservice
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)
Ii. using JDK to develop webservice
2.1, Development WebService server side

1. Define a interface, use the @webservice annotation callout interface, and use the @webmethod annotation to label all methods defined in the interface as follows:
Copy Code

1 package me.gacl.ws;
2
3 Import Javax.jws.WebMethod;
4 Import Javax.jws.WebService;
5
6/**
7 * @author GaCl
8 * Definition sei (WebService EndPoint Interface (terminal))
9 */
10//Use @webservice annotation Callout Webservicei interface
@WebService
Public interface Webservicei {
13
14//Use @webmethod annotations to annotate methods in the Webservicei interface
@WebMethod
String SayHello (string name);
17
@WebMethod
String Save (string name,string pwd);
20}

Copy Code

2, write interface implementation class, use the @webservice annotation annotation implementation class, implement all the methods defined in the interface, as follows:
Copy Code

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 annotations to label the implementation class of the WEBSERVICEI interface Webserviceimpl
Ten @WebService
public class Webserviceimpl implements WEBSERVICEI {
12
@Override
+ public string SayHello (string name) {
System.out.println ("WebService SayHello" +name);
Return "SayHello" +name;
17}
18
@Override
public string Save (string name, string pwd) {
System.out.println ("WebService Save" +name+ "," +pwd);
Return "Save Success";
23}
24}

Copy Code

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

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 * Publish Web Service
11 */
public class Webservicepublish {
13
public static void Main (string[] args) {
15//Define WebService address, this address is provided to the outside world to access Webervice URL address, the URL address format is: HTTP://IP: port number/xxxx
//string address = "http://192.168.1.100:8989/"; the webservice is legal.
//string address = "Http://192.168.1.100:8989/Webservice"; this Webservice is a legitimate issue.
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 to ensure that the port number used is not occupied by other applications when publishing
Endpoint.publish (Address, New Webserviceimpl ());
System.out.println ("Release WebService success!");
22}
23}

Copy Code

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
Copy Code

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/**
Ten * @author GaCl
11 * Listener for publishing WebService
12 */
13//Using the @weblistener annotation provided by Servlet3.0 will implement the Webservicepublishlistener class of the Servletcontextlistener interface labeled as a listener
@WebListener
public class Webservicepublishlistener implements Servletcontextlistener {
16
@Override
public void contextdestroyed (Servletcontextevent sce) {
19
20}
21st
@Override
contextinitialized public void (Servletcontextevent SCE) {
Release address of the//webservice
String address = "Http://192.168.1.100:8080/WS_Server/WebService";
26//Release Webservice,webserviceimpl class is the specific implementation class of the Webservie interface
Endpoint.publish (Address, New Webserviceimpl ());
System.out.println ("Use Webservicepublishlistener to publish WebService success!");
29}
30}

Copy Code

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
Copy Code

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/**
Ten * @author GaCl
11 * servlet for publishing WebService
12 */
13//Use @webservlet annotations provided with Servlet3.0 to label the generic Java class that inherits the HttpServlet class as a servlet
14//Set the Value property to an empty string so that Webservicepublishservlet does not provide a path to external access
The//loadonstartup property sets the initialization time of the Webservicepublishservlet
@WebServlet (value= "", Loadonstartup=0)
public class Webservicepublishservlet extends HttpServlet {
18
*/* (Non-javadoc)
* @see Javax.servlet.genericservlet#init ()
21 * Released at Webservicepublishservlet initialization WebService
22 */
All public void init () throws Servletexception {
Release address of the//webservice
String address = "Http://192.168.1.100:8888/WebService";
26//Release Webservice,webserviceimpl 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}

Copy Code

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:
Copy Code

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 * Client Calling WebService
9 */
public class WSClient {
11
public static void Main (string[] args) {
13//Create a factory for generating Webserviceimpl instances, the Webserviceimplservice class is generated by the Wsimport tool
Webserviceimplservice factory = new Webserviceimplservice ();
15//Generate a Webserviceimpl instance from the factory, Webserviceimpl is generated by the Wsimport tool
Webserviceimpl Wsimpl = Factory.getwebserviceimplport ();
17//Call the SayHello method of WebService
String Resresult = Wsimpl.sayhello ("Aloof pale Wolf");
System.out.println ("Call WebService's SayHello method to return the result is:" +resresult);
System.out.println ("---------------------------------------------------");
21//Call the Save method of WebService
Resresult = Wsimpl.save ("Aloof and pale Wolf", "123");
System.out.println ("Call WebService's Save method returns the result:" +resresult);
24}
25}

Copy Code

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 Learning Summary (iii)--using JDK to develop 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.