首页 JAX-WS自学笔记

JAX-WS自学笔记

举报
开通vip

JAX-WS自学笔记JAX-WS使用教程 1、JAX-WS概述 JAX-WS 2.0 的全称为 Java API for XML-Based Web services (JAX-WS) 2.0。JAX-WS 2.0 是对 JAX-RPC 1.0 规范的扩展,是 JAX-RPC 1.1 的后续版本, JAX-RPC 2.0标准发布不久后便被重新命名为 JAX-WS 2.0。 JAX-WS 2.0 是面向 Java 5 的开发 Web services 的最新编程标准,它提供了新的编程模型和对以往的 JAX-RPC 方式的 Web ser...

JAX-WS自学笔记
JAX-WS使用教程 1、JAX-WS概述 JAX-WS 2.0 的全称为 Java API for XML-Based Web services (JAX-WS) 2.0。JAX-WS 2.0 是对 JAX-RPC 1.0 规范的扩展,是 JAX-RPC 1.1 的后续版本, JAX-RPC 2.0标准发布不久后便被重新命名为 JAX-WS 2.0。 JAX-WS 2.0 是面向 Java 5 的开发 Web services 的最新编程标准,它提供了新的编程模型和对以往的 JAX-RPC 方式的 Web services 进行了增强。 JAX-WS2.0 (JSR 224)是Sun新的web services协议栈,是一个完全基于标准的实现。在binding层,使用的是the Java Architecture for XML Binding (JAXB, JSR 222),在parsing层,使用的是the Streaming API for XML (StAX, JSR 173),同时它还完全支持schema规范。 2、创建Web Service JAX-WS 2.0 有两种开发过程:自顶向下和自底向上。自顶向下方式指通过一个 WSDL 文件来创建Web Service,自底向上是从 Java 类出发创建 Web Service。两种开发过程最终形成的文件包括: SEI。一个SEI对应WSDL中Web Service的一个port,在Java中是一个Java接口。 SEI实现类。 WSDL和XSD文件。 2.1 从java开始 编写类Hello 代码如下: package ws; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class Hello { @WebMethod public String say(String name , int age){ return "Hello , I am "+name+", I am "+age+" years old"; } } 根据这webservice类,生成wsdl和服务端代码 2.1.1 运行wsgen wsgen -cp ./bin -r ./wsdl -s ./src -d ./bin -wsdl ws.Hello 解释: · -cp 定义classpath,就是ws.Hello这个类的classes文件存放的地方(如:bin\ws\Hello.class) · -r 生成 bean的wsdl文件的存放目录,即指定wsdl存放地方 · -s 生成发布Web Service的源代码文件的存放目录(如果方法有抛出异常,则会生成该异常的描述类源文件) · -d 生成发布Web Service的编译过的二进制类文件的存放目录(该异常的描述类的class文件),即生成的Say和SayResponse的class文件存放处 生成的java代码: package ws.jaxws; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "say", namespace = "http://ws/") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "say", namespace = "http://ws/", propOrder = { "arg0", "arg1" }) public class Say { @XmlElement(name = "arg0", namespace = "") private String arg0; @XmlElement(name = "arg1", namespace = "") private int arg1; /** * * @return * returns String */ public String getArg0() { return this.arg0; } /** * * @param arg0 * the value for the arg0 property */ public void setArg0(String arg0) { this.arg0 = arg0; } /** * * @return * returns int */ public int getArg1() { return this.arg1; } /** * * @param arg1 * the value for the arg1 property */ public void setArg1(int arg1) { this.arg1 = arg1; } } 应答代码 package ws.jaxws; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "sayResponse", namespace = "http://ws/") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "sayResponse", namespace = "http://ws/") public class SayResponse { @XmlElement(name = "return", namespace = "") private String _return; /** * * @return * returns String */ public String getReturn() { return this._return; } /** * * @param _return * the value for the _return property */ public void setReturn(String _return) { this._return = _return; } } 2.1.2 生成的WSDL和XSD WSDL XSD 2.1.3 目录结构 目录结构 2.2 从WSDL开始 2.2.1 运行wsimport 使用上面生成的WSDL来反向生成Java代码。生成后,不需要进行改动。 wsimport -d ./bin -s ./src -p test.ws D:\MyEclipseNGCRMWorkspace\jax-ws\wsdl\HelloService.wsdl 目录结构 2.2.2 生成的java代码 Hello.java package test.ws; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.1 in JDK 6 * Generated source version: 2.1 * */ @WebService(name = "Hello", targetNamespace = "http://ws/") @XmlSeeAlso({ ObjectFactory.class }) public interface Hello { /** * * @param arg1 * @param arg0 * @return * returns java.lang.String */ @WebMethod @WebResult(targetNamespace = "") @RequestWrapper(localName = "say", targetNamespace = "http://ws/", className = "test.ws.Say") @ResponseWrapper(localName = "sayResponse", targetNamespace = "http://ws/", className = "test.ws.SayResponse") public String say( @WebParam(name = "arg0", targetNamespace = "") String arg0, @WebParam(name = "arg1", targetNamespace = "") int arg1); } HelloService.java package test.ws; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebEndpoint; import javax.xml.ws.WebServiceClient; import javax.xml.ws.WebServiceFeature; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.1 in JDK 6 * Generated source version: 2.1 * */ @WebServiceClient(name = "HelloService", targetNamespace = "http://ws/", wsdlLocation = "file:/D:/MyEclipseNGCRMWorkspace/jax-ws/wsdl/HelloService.wsdl") public class HelloService extends Service { private final static URL HELLOSERVICE_WSDL_LOCATION; static { URL url = null; try { url = new URL("file:/D:/MyEclipseNGCRMWorkspace/jax-ws/wsdl/HelloService.wsdl"); } catch (MalformedURLException e) { e.printStackTrace(); } HELLOSERVICE_WSDL_LOCATION = url; } public HelloService(URL wsdlLocation, QName serviceName) { super(wsdlLocation, serviceName); } public HelloService() { super(HELLOSERVICE_WSDL_LOCATION, new QName("http://ws/", "HelloService")); } /** * * @return * returns Hello */ @WebEndpoint(name = "HelloPort") public Hello getHelloPort() { return (Hello)super.getPort(new QName("http://ws/", "HelloPort"), Hello.class); } /** * * @param features * A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the features parameter will have their default values. * @return * returns Hello */ @WebEndpoint(name = "HelloPort") public Hello getHelloPort(WebServiceFeature... features) { return (Hello)super.getPort(new QName("http://ws/", "HelloPort"), Hello.class, features); } } ObjectFactory.java package test.ws; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlElementDecl; import javax.xml.bind.annotation.XmlRegistry; import javax.xml.namespace.QName; /** * This object contains factory methods for each * Java content interface and Java element interface * generated in the test.ws package. *

An ObjectFactory allows you to programatically * construct new instances of the Java representation * for XML content. The Java representation of XML * content can consist of schema derived interfaces * and classes representing the binding of schema * type definitions, element declarations and model * groups. Factory methods for each of these are * provided in this class. * */ @XmlRegistry public class ObjectFactory { private final static QName _Say_QNAME = new QName("http://ws/", "say"); private final static QName _SayResponse_QNAME = new QName("http://ws/", "sayResponse"); /** * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: test.ws * */ public ObjectFactory() { } /** * Create an instance of {@link Say } * */ public Say createSay() { return new Say(); } /** * Create an instance of {@link SayResponse } * */ public SayResponse createSayResponse() { return new SayResponse(); } /** * Create an instance of {@link JAXBElement }{@code <}{@link Say }{@code >}} * */ @XmlElementDecl(namespace = "http://ws/", name = "say") public JAXBElement createSay(Say value) { return new JAXBElement(_Say_QNAME, Say.class, null, value); } /** * Create an instance of {@link JAXBElement }{@code <}{@link SayResponse }{@code >}} * */ @XmlElementDecl(namespace = "http://ws/", name = "sayResponse") public JAXBElement createSayResponse(SayResponse value) { return new JAXBElement(_SayResponse_QNAME, SayResponse.class, null, value); } } package-info.java @javax.xml.bind.annotation.XmlSchema(namespace = "http://ws/") package test.ws; Say.java package test.ws; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType; /** *

Java class for say complex type. * *

The following schema fragment specifies the expected content contained within this class. * *


 * <complexType name="say">

 *   <complexContent>

 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">

 *       <sequence>

 *         <element name="arg0" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>

 *         <element name="arg1" type="{http://www.w3.org/2001/XMLSchema}int"/>

 *       </sequence>

 *     </restriction>

 *   </complexContent>

 * </complexType>

 * 
* * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "say", propOrder = { "arg0", "arg1" }) public class Say { protected String arg0; protected int arg1; /** * Gets the value of the arg0 property. * * @return * possible object is * {@link String } * */ public String getArg0() { return arg0; } /** * Sets the value of the arg0 property. * * @param value * allowed object is * {@link String } * */ public void setArg0(String value) { this.arg0 = value; } /** * Gets the value of the arg1 property. * */ public int getArg1() { return arg1; } /** * Sets the value of the arg1 property. * */ public void setArg1(int value) { this.arg1 = value; } } SayResponse.java package test.ws; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; /** *

Java class for sayResponse complex type. * *

The following schema fragment specifies the expected content contained within this class. * *


 * <complexType name="sayResponse">

 *   <complexContent>

 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">

 *       <sequence>

 *         <element name="return" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>

 *       </sequence>

 *     </restriction>

 *   </complexContent>

 * </complexType>

 * 
* * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "sayResponse", propOrder = { "_return" }) public class SayResponse { @XmlElement(name = "return") protected String _return; /** * Gets the value of the return property. * * @return * possible object is * {@link String } * */ public String getReturn() { return _return; } /** * Sets the value of the return property. * * @param value * allowed object is * {@link String } * */ public void setReturn(String value) { this._return = value; } } 创建SEI类 实现Hello接口,并添加@WebService注释,并至少为@WebService添加endpointInterface属性。 package test.ws; import javax.jws.WebService; @WebService(serviceName = "HelloService", portName = "HelloPort", endpointInterface = "test.ws.Hello", targetNamespace = "http://ws/") public class HelloSEI implements Hello { @Override public String say(String arg0, int arg1) { return "Hello , I am "+arg0+", I am "+arg1+" years old"; } } 生成服务端要领:定义一个发布接口类,该接口类实现portType里定义的接口,重写该方法即可。在WebService里定义的serviceName = "HelloService"是服务,protName是服务里定义的端口,endpointInterface是指发布的接口,该接口即是实现的接口,即在portType里定义的操作类接口,targetNamespace是定义的域名,跟wsdl相关,建议修改wsdl里的域名,自定义域名即可. 2.3发布Web Service 2.3.1在应用程序中发布 JDK6提供了发布Web Service的简便方法: Endpoint.publish("http://localhost:8080/HelloService", new Hello()); 如果是从WSDL生成的Web Service,则为, Endpoint.publish("http://localhost:8080/HelloService", new HelloSEI()); 代码如下: package test; import javax.xml.ws.Endpoint; import test.ws.HelloSEI; public class TestHello { public static void main(String[] args) { Endpoint.publish("http://localhost:8080/HelloService", new HelloSEI()); } } 2.3.2在Web应用程序中发布 利用SUN公司提供的辅助包,可以将Web Service发布为Web应用程序。 2.3.2.1 依赖包 activation.jar FastInfoset.jar http.jar jaxb-impl.jar jaxws-rt.jar mimepull.jar resolver.jar stax-ex.jar streambuffer.jar 2.3.2.1发布步骤 1) 修改web.xml,添加 com.sun.xml.ws.transport.http.servlet.WSServletContextListener Hello com.sun.xml.ws.transport.http.servlet.WSServlet 2 Hello /HelloService 为每一个WebService声明一个com.sun.xml.ws.transport.http.servlet.WSServlet。 2) 在WEB-INF目录下新建sun-jaxws.xml文件,内容如下, 将ws.server.fromjava.Hello声明为Web Service。 如果是从WSDL生成的Web Service,则为, 2.4 创建Web Service客户端 客户端开发的通常过程是从已有的WSDL出发,创建辅助类JAXB对象和Service代理类,然后基于这些类开发自己的客户端应用。 2.4.1同步调用方式的客户端 2.4.1.1 开发步骤 1) 运行wsimport命令,生成客户端代码; 2) 修改生成的Java代码中的WSDL地址。 2.4.1.2 运行wsimport wsimport -d ./bin -s ./src -p test.ws D:\MyEclipseNGCRMWorkspace\jax-ws\wsdl\HelloService.wsdl src是生成的源代码的存放路径。 bin是生成的class文件的存放路径。HelloService.wsdl是Web Service WSDL的路径。 详情请参考2.2节 2.4.1.3 修改生成的Java代码 编辑HelloService.java,将 package test.ws; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebEndpoint; import javax.xml.ws.WebServiceClient; import javax.xml.ws.WebServiceFeature; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.1 in JDK 6 * Generated source version: 2.1 * */ @WebServiceClient(name = "HelloService", targetNamespace = "http://ws/", wsdlLocation = "file:/D:/MyEclipseNGCRMWorkspace/jax-ws/wsdl/HelloService.wsdl") public class HelloService extends Service { private final static URL HELLOSERVICE_WSDL_LOCATION; static { URL url = null; try { url = new URL("file:/D:/MyEclipseNGCRMWorkspace/jax-ws/wsdl/HelloService.wsdl"); } catch (MalformedURLException e) { e.printStackTrace(); } HELLOSERVICE_WSDL_LOCATION = url; } public HelloService(URL wsdlLocation, QName serviceName) { super(wsdlLocation, serviceName); } public HelloService() { super(HELLOSERVICE_WSDL_LOCATION, new QName("http://ws/", "HelloService")); } /** * * @return * returns Hello */ @WebEndpoint(name = "HelloPort") public Hello getHelloPort() { return (Hello)super.getPort(new QName("http://ws/", "HelloPort"), Hello.class); } /** * * @param features * A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the features parameter will have their default values. * @return * returns Hello */ @WebEndpoint(name = "HelloPort") public Hello getHelloPort(WebServiceFeature... features) { return (Hello)super.getPort(new QName("http://ws/", "HelloPort"), Hello.class, features); } } 修改为, package test.ws; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebEndpoint; import javax.xml.ws.WebServiceClient; import javax.xml.ws.WebServiceFeature; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.1 in JDK 6 * Generated source version: 2.1 * */ @WebServiceClient(name = "HelloService", targetNamespace = "http://ws/") public class HelloService extends Service { private final static URL HELLOSERVICE_WSDL_LOCATION; public HelloService(URL wsdlLocation){ //wsdl地址由构造函数提供,非指定 super(wsdlLocation, new QName("http://ws/", "HelloService")); } static { URL url = null; try { url = new URL("file:/D:/MyEclipseNGCRMWorkspace/jax-ws/wsdl/HelloService.wsdl"); } catch (MalformedURLException e) { e.printStackTrace(); } HELLOSERVICE_WSDL_LOCATION = url; } public HelloService(URL wsdlLocation, QName serviceName) { super(wsdlLocation, serviceName); } public HelloService() { super(HELLOSERVICE_WSDL_L
本文档为【JAX-WS自学笔记】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_942963
暂无简介~
格式:doc
大小:128KB
软件:Word
页数:21
分类:互联网
上传时间:2012-07-25
浏览量:10