WebService-01-使用JAX-WS发布WebService服务

来源:互联网 发布:discuz源码安装 编辑:程序博客网 时间:2024/05/20 20:05

一、写在前面的话


近来项目中使用WebService技术,从早期的Axis2到现在的CXF,闲来无事,做做笔记,以备后用,后续会写CXF的基本用法、拦截器、Map类型在WebService的使用、通过自定义拦截器实现权限的验证等。


JAX-WS位于JDK的javax.jws包下,主要包括@WebService,@WebMethod等几个注解,使用这些注解,便可发布WebService服务,被发布成服务的类可以是一个最简单的类,也可以是某个接口的实现类,即“接口+实现”的方式,笔记中的示例代码均为“接口+实现”的方式。以HelloWorld接口为例,接口中两个方法:sayHi,sayHello,简介的说一下@WebService和@WebMethod的使用。


二、服务端(@WebService)


接口代码:

package com.wds.ws.server.helloworld;import javax.jws.WebService;/** * @WebService 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口 *  * @author wds * */@WebServicepublic interface HelloWorld {public String sayHi(String name);public String sayHello(String name);}

提供一个实现类,同时实现sayHi,sayHello的方法,实现类代码如下:

package com.wds.ws.server.helloworld;import java.util.Date;import javax.jws.WebService;/** * 如果注解中填写了endpointInterface这个属性,那么这个属性值所指定的接口上,也必须有@webservice的注解 *  * portName指定了wsdl中port的名称 *  * serviceName指定了wsdl中服务的名称 *  * @author wds */@WebService(endpointInterface="com.wds.ws.server.helloworld.HelloWorld", portName="hwPort", serviceName="hw")public class HelloWorldWs implements HelloWorld{public String sayHello(String name) {System.out.println(new Date() + " sayHello " + name);return name + ", Hello World!";}public String sayHi(String name) {System.out.println(new Date() + " sayHi " + name);return name  + ", welcome webservice world!";}}
如果将HelloWorldWs发布出去呢,采用Endpoint的方式,代码如下:

package com.wds.ws.server.helloworld;import javax.xml.ws.Endpoint;/** * 服务发布类 *  * @author wds * */public class Publish {/** * @param args */public static void main(String[] args) {Endpoint.publish("http://localhost:567/helloworld", new HelloWorldWs());}}
运行此类,在浏览器中输入http://localhost:567/helloworld?wsdl,如果正常打开,说明发布服务成功,其wsdl文件内容如下:

  <?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.2.4-b01.   --> - <!--  Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.   --> - <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://helloworld.server.ws.wds.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://helloworld.server.ws.wds.com/" name="hw">- <types>- <xsd:schema>  <xsd:import namespace="http://helloworld.server.ws.wds.com/" schemaLocation="http://localhost:567/helloworld?xsd=1" />   </xsd:schema>  </types>- <message name="sayHello">  <part name="parameters" element="tns:sayHello" />   </message>- <message name="sayHelloResponse">  <part name="parameters" element="tns:sayHelloResponse" />   </message>- <message name="sayHi">  <part name="parameters" element="tns:sayHi" />   </message>- <message name="sayHiResponse">  <part name="parameters" element="tns:sayHiResponse" />   </message>- <portType name="HelloWorld">- <operation name="sayHello">  <input wsam:Action="http://helloworld.server.ws.wds.com/HelloWorld/sayHelloRequest" message="tns:sayHello" />   <output wsam:Action="http://helloworld.server.ws.wds.com/HelloWorld/sayHelloResponse" message="tns:sayHelloResponse" />   </operation>- <operation name="sayHi">  <input wsam:Action="http://helloworld.server.ws.wds.com/HelloWorld/sayHiRequest" message="tns:sayHi" />   <output wsam:Action="http://helloworld.server.ws.wds.com/HelloWorld/sayHiResponse" message="tns:sayHiResponse" />   </operation>  </portType>- <binding name="hwPortBinding" type="tns:HelloWorld">  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <operation name="sayHello">  <soap:operation soapAction="" /> - <input>  <soap:body use="literal" />   </input>- <output>  <soap:body use="literal" />   </output>  </operation>- <operation name="sayHi">  <soap:operation soapAction="" /> - <input>  <soap:body use="literal" />   </input>- <output>  <soap:body use="literal" />   </output>  </operation>  </binding>- <service name="hw">- <port name="hwPort" binding="tns:hwPortBinding">  <soap:address location="http://localhost:567/helloworld" />   </port>  </service>  </definitions>

查看上述的XML文档,其中service name和port name便是我们在代码中设置的值。


三、客户端


在编写完服务端之后,我们编写客户端,JDK给我们提供了一个命令wsimport命令帮助我们生成客户端代码(具体使用方式,可百度、谷歌),使用以下命令方式:

wsimport -s .  http://localhost:567/helloworld?wsdl

其中.代表当前目录,可以选择任意目录,在生成的代码中包含了.clss的文件,删除,将生成的Java文件导入到工程当中。

工程图如下:


编写测试类,代码如下:

package test;import com.wds.ws.server.helloworld.HelloWorld;import com.wds.ws.server.helloworld.Hw;/** * WebService客户端测试类 * @author wds * */public class Test {/** * @param args */public static void main(String[] args) {//service对应于wsdl文件中的service节点中的nameHw hwws = new Hw();//通过service获取port,port的Name对应了wsdl文件中port节点的nameHelloWorld ws = hwws.getHwPort();//执行方法String result = ws.sayHi("wds");//输出结果System.out.println(result);System.out.println(ws.sayHello("hello"));}}
每执行一次结果,服务端和客户端都输出结果,下图为服务端的输出结果,我执行两次。

至此,服务端和客户端已经完成。


四、@WebMethod


在服务端中,我们分别写了sayHi和sayHello,在某些场景中,我们可能只是想暴露其中的一个方法,肿么办呢?我们就要使用@WebMethod,下面将sayHello的方法去掉。

HelloWorld的代码如下:

package com.wds.ws.server.helloworld;import javax.jws.WebMethod;import javax.jws.WebService;/** * @WebService 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口 *  * @author wds * */@WebServicepublic interface HelloWorld {public String sayHi(String name);/** * 通过@WebMethod注解,将exclude的属性设置为true,则可将此方法不暴露为webservice * @param name * @return */@WebMethod(exclude=true)public String sayHello(String name);}
再Publish重新执行一次,(注:@WebMethod如果放到实现类的方法上去,不起作用,应该放到接口的方法中)观察其生成的wsdl文件
  <?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.2.4-b01.   --> - <!--  Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.   --> - <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://helloworld.server.ws.wds.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://helloworld.server.ws.wds.com/" name="hw">- <types>- <xsd:schema>  <xsd:import namespace="http://helloworld.server.ws.wds.com/" schemaLocation="http://localhost:567/helloworld?xsd=1" />   </xsd:schema>  </types>- <message name="sayHi">  <part name="parameters" element="tns:sayHi" />   </message>- <message name="sayHiResponse">  <part name="parameters" element="tns:sayHiResponse" />   </message>- <portType name="HelloWorld">- <operation name="sayHi">  <input wsam:Action="http://helloworld.server.ws.wds.com/HelloWorld/sayHiRequest" message="tns:sayHi" />   <output wsam:Action="http://helloworld.server.ws.wds.com/HelloWorld/sayHiResponse" message="tns:sayHiResponse" />   </operation>  </portType>- <binding name="hwPortBinding" type="tns:HelloWorld">  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <operation name="sayHi">  <soap:operation soapAction="" /> - <input>  <soap:body use="literal" />   </input>- <output>  <soap:body use="literal" />   </output>  </operation>  </binding>- <service name="hw">- <port name="hwPort" binding="tns:hwPortBinding">  <soap:address location="http://localhost:567/helloworld" />   </port>  </service>  </definitions>
很显然,在operation中,少了一个name为sayHello的节点,当然客户再次调用sayHello的方法时也会报出以下的异常信息。

Exception in thread "main" javax.xml.ws.WebServiceException: Method sayHello is exposed as WebMethod, but there is no corresponding wsdl operation with name sayHello in the wsdl:portType{http://helloworld.server.ws.wds.com/}HelloWorldat com.sun.xml.internal.ws.model.JavaMethodImpl.freeze(JavaMethodImpl.java:341)at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.freeze(AbstractSEIModelImpl.java:94)at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:241)at com.sun.xml.internal.ws.client.WSServiceDelegate.createSEIPortInfo(WSServiceDelegate.java:652)at com.sun.xml.internal.ws.client.WSServiceDelegate.addSEI(WSServiceDelegate.java:640)at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:332)at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:315)at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:297)at javax.xml.ws.Service.getPort(Service.java:119)at com.wds.ws.server.helloworld.Hw.getHwPort(Hw.java:72)at test.Test.main(Test.java:22)


五、小结


通过JDK为我们提供的@WebService、@WebMethod发布服务,并能将类中的一些方法屏蔽,通过wsimport命令可以生成客户端代码。