Implementation of WebService based on Jax-WS specification

Source: Internet
Author: User
Tags wsdl

1. Related Introduction

Introducing Web service requires understanding SOA first. SOA (service-oriented Architecture) service-oriented architecture is an idea that connects different functional units of an application through an intermediary contract (independent of the hardware platform, operating system, and programming language), allowing for better integration of functional units of various forms. WebService is a good way to implement SOA, WebService uses HTTP as the transport protocol, and SOAP (simple Object Access Protocol) as the format for transmitting messages.
This paper introduces WebService implementation method based on JAX-WS specification. First we understand what Jax-ws is.
JAX-WS (Java API for XML Web services) is an API for creating WEB services in the Java programming language. On the server side, the user simply needs to define the interface Sei (Service endpoint interface) required by the remote call through the Java language and provide the relevant implementation that can be published as a WebService interface by invoking the Jax-WS service Publishing interface. On the client side, the user can create a proxy (using a local object instead of a remote service) through the JAX-WS API to implement a call to the remote server side. Of course, Jax-WS also provides a set of API calls that operate on the underlying messages, and you can use dispatch to send requests directly using SOAP messages or XML messages or to process SOAP or XML messages using provider.

"Reproduced use, please specify the source:http://blog.csdn.net/mahoking"

2, using the general way to achieve the webservice based on Jax-WS specification

This example takes the MyEclipse development tool as an example, the server is Tomcat 6, demonstrating the general way of implementing the WebService based on the Jax-WS specification.
First step: Open Myeclipse,new→web Service Project. The framework chooses JAX-ws. This example project is named Webserver.
Second step: Create a new package name cn.mahaochen.web.ws, under which the class SayHello is established.

Package Cn.mahaochen.web.ws;public class SayHello {public string SayHello (String name) {return "Welcome" + name + "Come h Ere! ";}}

Step three: New→other→web services→web Service. Next,java Package Select the Cn.mahaochen.web.ws,next we created earlier. This myeclipse automatically generates 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 ();p ublic string SayHello (string name) {return Sayhello.sayhello (name);}}


At the same time, a new XML file is generated under the Web-inf file as: Sun-jaxws.xml.

<?xml Version = "1.0"? ><endpoints version= "2.0" xmlns= "Http://java.sun.com/xml/ns/jax-ws/ri/runtime" > <endpoint name= "Sayhelloport" implementation= "Cn.mahaochen.web.ws.SayHelloDelegate" url-pattern= "/sayhelloport "></endpoint></endpoints>

The content in Web. XML also changes.

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> <display-name></display-name> <servlet> < Description>jax-ws endpoint-sayhelloservice</description> <display-name>sayhelloservice</  Display-name> <servlet-name>SayHelloService</servlet-name> <servlet-class> Com.sun.xml.ws.transport.http.servlet.WSServlet </servlet-class> <load-on-startup>1</ load-on-startup> </servlet> <servlet-mapping> <servlet-name>sayhelloservice</servlet-name > <url-pattern>/SayHelloPort</url-pattern> </servlet-mapping> <welcome-file-list> < welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> &LT;LISTENER-CLASS&GT Com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener> </ Web-app>


step three: publish (deploy) the project to Tomcat, start the service, access the test, the following message appears.

<?xml version= "1.0" encoding= "UTF-8"?><!--Published by Jax-ws RI at http://jax-ws.dev.java.net. Ri ' s version is JAX-ws RI 2.1.3-hudson-390-. --><!--Generated by Jax-ws RI at http://jax-ws.dev.java.net. Ri ' s version is JAX-ws RI 2.1.3-hudson-390-. --><definitions xmlns:wsu= "http://docs.oasis-open.org/wss/2004/01/ Oasis-200401-wss-wssecurity-utility-1.0.xsd "xmlns:soap=" http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns=" http ://ws.web.mahaochen.cn/"xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "xmlns=" http://schemas.xmlsoap.org/wsdl/" Targetnamespace= "http://ws.web.mahaochen.cn/" name= "Sayhelloservice" ><types><xsd:schema><xsd: Import namespace= "http://ws.web.mahaochen.cn/" schemalocation= "http://127.0.0.1:80/WSServer2/SayHelloPort?xsd=1" ></xsd:import></xsd:schema></types><message name= "SayHello" ><part name= "parameters "Element=" Tns:sayhello "></part></message><message name=" Sayhelloresponse "><part naMe= "Parameters" element= "Tns:sayhelloresponse" ></part></message><porttype name= " Sayhellodelegate "><operation name=" SayHello "><input message=" Tns:sayhello "></input>< Output message= "Tns:sayhelloresponse" ></output></operation></porttype><binding name= " Sayhelloportbinding "type=" tns:sayhellodelegate "><soap:binding transport=" http://schemas.xmlsoap.org/soap/ HTTP "style=" document "></soap:binding><operation name=" SayHello "><soap:operation soapaction=" " ></soap:operation><input><soap:body use= "literal" ></soap:body></input>< Output><soap:body use= "literal" ></soap:body></output></operation></binding> <service name= "Sayhelloservice" ><port name= "Sayhelloport" binding= "tns:sayhelloportbinding" ><soap: Address location= "Http://127.0.0.1:80/WSServer2/SayHelloPort" ></soap:address></port></service ></definitions&gT 

3. Using annotations/annotations to implement WebService based on Jax-WS specification

The Java API for XML-based Web service (JAX-WS) uses annotations/annotations to specify the metadata associated with a Web service implementation and to simplify the development steps of a Web service.
The main annotation/annotation classes covered in this case are as follows:

Class How to use Role
Javax.jws.WebService @WebService When the Web service is implemented, the Java class is marked @WebService comment, and the service Endpoint interface (SEI) is marked when the Web service interface is implemented.
Javax.jws.WebMethod @WebMethod @WebMethod annotation represents a method as a Web Service operation.
Apply this comment to a method on the client or Server service endpoint interface (SEI), or to the server endpoint implementation class that is applied to the JavaBeans endpoint.
Javax.jws.WebParam @WebParam @WebParam annotations are used to customize mappings from a single parameter to a WEB Service message part and XML element.
Apply this comment to a method on the client or Server service endpoint interface (SEI), or to the server endpoint implementation class that is applied to the JavaBeans endpoint.


In addition to the above annotation/annotation classes include Javax.jws.Oneway, Javax.jws.WebResult, Javax.jws.HandlerChain, javax.jws.SOAPBinding, and so on.
Please read the following address for more information.
http://www-01.ibm.com/support/knowledgecenter/SSAW57_6.1.0/com.ibm.websphere.wsfep.multiplatform.doc/info/ae/ Ae/rwbs_jaxwsannotations.html?lang=zh

"reprint use, please specify the source: http://blog.csdn.net/mahoking"

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.