使用JDK 6 编写 Web service
?
服务器端的Java类若要成为一个实现了Web Service的bean,它需要遵循下边这些原则:这个类必须是public类、不能是final的或者abstract、必须有一个公共的默认构造函数、绝对不能有finalize()方法。若要成为一个实现了Web Service的Bean的方法必须遵循这些原则:package test.jws.service;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;@WebService(targetNamespace = "http://www.jwstest.org")@SOAPBinding(style = SOAPBinding.Style.RPC)publicclass HelloWorld { @WebMethod(action="toSayHello",operationName="toSayHello",exclude=false) @WebResult(name="returnWord")//自定义该方法返回值在WSDL中相关的描述 public String sayHello(@WebParam(name="userName")String userName) { return"Hello:" + userName; } @WebMethod publicint getExp(int i, int j) { return i / j; }}?这是服务器端普通的业务类,通过@WebService、@WebMethod等注释描述来生成WSDL文件。STEP 2,执行wsgen命令本例中到HelloWorld类所在的目录中新建一个命名为wsdl的文件夹,运行:wsgen -cp ./bin -r ./wsdl -s ./src -d ./bin -wsdl test.jws.service.HelloWorld。执行后会在wsdl文件夹中生成HelloWorld的wsdl描述文件,src文件夹中生成依赖类,如异常说明类,bin中生成依赖类的class文件STEP 3,发布Web Service Bean启动服务类StartService.java:package test.jws.service;import javax.xml.ws.Endpoint;publicclass StartService { publicstaticvoid main(String[] args) { Endpoint.publish("http://localhost:8080/webservice/hws", new HelloWorld()); }}? ? ?此类很简单,能过Endpoint类的publish()方法发布实例发布地址为:http://localhost:8080/webservice/hws,必需明确指明http协议,主机IP 地址及端口号,在IE上输入?http://localhost:8080/webservice/hws?wsdl返回以下内容说明发布成功<?xml version="1.0" encoding="UTF-8" ?><definitions xmlns="http://schemas.xmlsoap.org/wsdl/"xmlns:tns="http://www.jwstest.org"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"targetNamespace="http://www.jwstest.org"name="HelloWorldService"><types /><message name="toSayHello"><part name="userName" type="xsd:string" /></message><message name="toSayHelloResponse"><part name="returnWord" type="xsd:string" /></message><message name="getExp"><part name="arg0" type="xsd:int" /><part name="arg1" type="xsd:int" /></message><message name="getExpResponse"><part name="return" type="xsd:int" /></message><portType name="HelloWorld"><operation name="toSayHello" parameterOrder="userName"><input message="tns:toSayHello" /><output message="tns:toSayHelloResponse" /></operation><operation name="getExp" parameterOrder="arg0 arg1"><input message="tns:getExp" /><output message="tns:getExpResponse" /></operation></portType><binding name="HelloWorldPortBinding" type="tns:HelloWorld"><soap:binding style="rpc"transport="http://schemas.xmlsoap.org/soap/http" /><operation name="toSayHello"><soap:operation soapAction="toSayHello" /><input><soap:body use="literal"namespace="http://www.jwstest.org" /></input><output><soap:body use="literal"namespace="http://www.jwstest.org" /></output></operation><operation name="getExp"><soap:operation soapAction="" /><input><soap:body use="literal"namespace="http://www.jwstest.org" /></input><output><soap:body use="literal"namespace="http://www.jwstest.org" /></output></operation></binding><service name="HelloWorldService"><port name="HelloWorldPort"binding="tns:HelloWorldPortBinding"><soap:addresslocation="http://localhost:8080/webservice/hws" /></port></service></definitions>?
package test.jws.client;import test.jws.client.ref.*;publicclass ClientRun { /** *@paramargs */ publicstaticvoid main(String[] args) { HelloWorldService hws = new HelloWorldService(); HelloWorld hw = hws.getHelloWorldPort(); System.out.println(hw.getExp(9, 3)); System.out.println(hw.toSayHello("zhuoshiyao")); }}?