WebService implementation based on JAX-WS Specification
1. Introduction
To introduce Web services, you must first understand SOA. SOA (Service-Oriented Architecture) is an idea of Service-Oriented Architecture. It uses different functional units of an application through intermediate contracts (independent of hardware platforms, operating systems, and programming languages) to better integrate function units in various forms. WebService is a good implementation method of SOA. WebService uses HTTP as the transmission Protocol, and SOAP (Simple Object Access Protocol) as the format for transmitting messages.
This article mainly introduces the WebService Implementation Method Based on JAX-WS specification. First, let's first understand what JAX-WS is.
JAX-WS (Java API for XML Web Services) is an API used to create Web Services in Java programming language. On the server side, you only need to use Java to define the SEI (service endpoint interface) interface for remote calls and provide relevant implementation, you can publish a JAX-WS as a WebService interface by calling its service publishing interface. On the client, you can create a proxy (replace remote services with local objects) through the JAX-WS API to call the remote server. Of course, the JAX-WS also provides a set of API calls for operations on underlying messages, you can use Dispatch to send requests directly using SOAP messages or XML messages, or use providers to process SOAP or XML messages.
2. Using the general method to realize WebService Based on JAX-WS Specification
In this example, the MyEclipse development tool is used as an example, the server is Tomcat 6, and the general method of WebService Based on JAX-WS specification is demonstrated.
Step 1: Open MyEclipse, new → Web Service Project. Framework select JAX-WS. This project is named WebServer.
Step 2: Create a new package named cn. mahaochen. web. ws and create a class SayHello under the package.
package cn.mahaochen.web.ws;public class SayHello {public String sayHello(String name) {return Welcome + name + Come here!;}}
Step 3: new → Other → Web Services → Web Service. Next, Java package: Select cn. mahaochen. web. ws and Next. In this way, MyEclipse will automatically generate a class SayHelloDelegate. java on the cn. mahaochen. web. ws package.
package cn.mahaochen.web.ws;@javax.jws.WebService(targetNamespace = http://ws.web.mahaochen.cn/, serviceName = SayHelloService, portName = SayHelloPort)public class SayHelloDelegate {cn.mahaochen.web.ws.SayHello sayHello = new cn.mahaochen.web.ws.SayHello();public String sayHello(String name) {return sayHello.sayHello(name);}}
At the same time, a new XML file is generated under the WEB-INF file: sun-jaxws.xml.
The content in web. xml also changes.
JAX-WS endpoint - SayHelloService
SayHelloService
SayHelloService
com.sun.xml.ws.transport.http.servlet.WSServlet
1
SayHelloService
/SayHelloPort
index.jsp
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
Step 3: publish (deploy) The project to Tomcat, start the service, and access http: // 127.0.0.1/WSServer/SayHelloPort through a browser? The following message is displayed in the wsdl test.
3. Use annotation/annotation to realize WebService Based on JAX-WS Specification
XML-based Web Service Java API (JAX-WS) specifies the metadata associated with the Web Service implementation by using Annotations/Annotations and simplifies the Web Service development steps.
The main annotation/annotation classes involved in this case are as follows:
Class |
Usage |
Function |
Javax. jws. WebService |
@ WebService |
When implementing a Web Service, @ WebService annotation marks the Java class; when implementing a Web Service interface, it marks the Service endpoint interface (SEI ). |
Javax. jws. WebMethod |
@ WebMethod |
@ WebMethod annotation indicates a Web Service operation method. Apply this annotation to methods on the client or server service endpoint interface (SEI), or to the server endpoint implementation class of the JavaBeans endpoint. |
Javax. jws. WebParam |
@ WebParam |
@ WebParam annotation is used to customize the ing between a single parameter and a Web Service Message component and an XML element. Apply this annotation to methods on the client or server service endpoint interface (SEI), or to the server endpoint implementation class of the JavaBeans endpoint. |
In addition to the above annotation/annotation classes, it also includes javax. jws. Oneway, javax. jws. WebResult, javax. jws. HandlerChain, javax. jws. SOAPBinding, and so on.