一、jax-ws webservice的编写流程之代码优先
1、jax-ws webservice的编写流程之代码优先
之前写webservice程序基本上用axis或xfire,最近的一个项目用到了jax-ws,研究了一下,总结一下内容。webservice的实现一般有两种方式:代码优先和契约优先。所谓的契约优先就是在实现前,先定义一个数据交换格式,这样大家在实现的时候就有统一的规范可依,这对于大系统设计而言是非常必要的;本文先讲述的是代码优先的方式,具体步骤:
1、设计一个webservice的接口
package gjs.text.ws.inter; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; /** * 定义一个webservice接口 * @author gaojingsheng * */ @WebService public interface TestService { /** * 求和 * @param a 加数a * @param b 加数b * @return */ @WebResult(name = "sumResult") public int sum(@WebParam(name = "a") int a, @WebParam(name = "b") int b); /** * 添加一个人,返回被添加的人 * @param person * @return */ @WebResult(name = "person") public Person addPerson(@WebParam(name = "person") Person person); }
这里需要注意的是,你需要熟悉三个annotation:@WebService、@WebResult、@WebParam。@WebService指定服务断点接口(SEI);@WebResult指定返回的结果的包装的元素名称;@WebParam表示封装请求参数的名称
2、实现服务接口(SIB)
package gjs.text.ws.inter; import java.util.ArrayList; import java.util.List; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService(endpointInterface="gjs.text.ws.inter.TestService") public class TestServiceImpl implements TestService{ //先定义一个存储队列 private List<Person> personList = new ArrayList<Person>(); @Override @WebResult(name = "sumResult") public int sum(@WebParam(name = "a") int a, @WebParam(name = "b") int b) { return a+b; } @Override @WebResult(name = "person") public Person addPerson(@WebParam(name = "person") Person person) { this.personList.add(person); return person; } }
3、发布服务
package startService; import javax.xml.ws.Endpoint; import gjs.test.service.MyServiceImpl; /** * 用于启动webservice,启动方式就是用Endpoint的publish方法 * @author gaojingsheng * */ public class StartService { public static void main(String[] args) { //jaxws的发布服务的方式 Endpoint.publish("http://localhost:8889/testService", new TestServiceImpl()); } }
4、看看我们发布效果(wsdl文件)
This XML file does not appear to have any style information associated with it. The document tree is shown below. <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://inter.ws.text.gjs/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/"targetNamespace="http://inter.ws.text.gjs/" name="TestServiceImplService"> <types> <xsd:schema> <xsd:import namespace="http://inter.ws.text.gjs/" schemaLocation="http://localhost:8889/testService?xsd=1"/> </xsd:schema> </types> <message name="sum"> <part name="parameters" element="tns:sum"/> </message> <message name="sumResponse"> <part name="parameters" element="tns:sumResponse"/> </message> <message name="addPerson"> <part name="parameters" element="tns:addPerson"/> </message> <message name="addPersonResponse"> <part name="parameters" element="tns:addPersonResponse"/> </message> <portType name="TestService"> <operation name="sum"> <input message="tns:sum"/> <output message="tns:sumResponse"/> </operation> <operation name="addPerson"> <input message="tns:addPerson"/> <output message="tns:addPersonResponse"/> </operation> </portType> <binding name="TestServiceImplPortBinding" type="tns:TestService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="sum"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> <operation name="addPerson"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="TestServiceImplService"> <port name="TestServiceImplPort" binding="tns:TestServiceImplPortBinding"> <soap:address location="http://localhost:8889/testService"/> </port> </service> </definitions>
到此,一个简单的服务就搭建完成了。