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; } }
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()方法发布实例发布地址为:
<?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:address location="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")); } }