一、创建对外公布的接口
package webService;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding
public interface MyServiceInter {
@WebResult(name="addResult")
int add(@WebParam(name="a")int a,@WebParam(name="b")int b);
}
二、创建接口的实现类
package webService;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding
public class MyService implements MyServiceInter{
@Override
public int add(int a, int b) {
System.out.println("a+b="+(a+b));
return a+b;
}
}
三、创建服务器对外发布接口
package webService; import javax.xml.ws.Endpoint; public class Server { public static void main(String[] args) { String address = "http://localhost:8888/webService"; Endpoint.publish(address, new MyService()); System.out.println("publish success"); }
}
服务器必须全程启动状态才能进行下一步,启动后可开启浏览器访问地址:http://localhost:8888/webService?wsdl 显示如下:
<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://webService/ " xmlns:xsd ="http://www.w3.org/2001/XMLSchema " xmlns ="http://schemas.xmlsoap.org/wsdl/ " targetNamespace ="http://webService/ " name ="MyServiceService " >
<types>
<xsd:schema>
<xsd:import namespace ="http://webService/ " schemaLocation ="http://localhost:8888/webService?xsd=1 " />
</xsd:schema>
</types>
<message name ="add " >
<part name ="parameters " element ="tns:add " />
</message>
<message name ="addResponse " >
<part name ="parameters " element ="tns:addResponse " />
</message>
<portType name ="MyService " >
<operation name ="add " >
<input wsam:Action ="http://webService/MyService/addRequest " message ="tns:add " />
<output wsam:Action ="http://webService/MyService/addResponse " message ="tns:addResponse " />
</operation>
</portType>
<binding name ="MyServicePortBinding " type ="tns:MyService " >
<soap:binding transport ="http://schemas.xmlsoap.org/soap/http " style ="document " />
<operation name ="add " >
<soap:operation soapAction =" " />
<input>
<soap:body use ="literal " />
</input>
<output>
<soap:body use ="literal " />
</output>
</operation>
</binding>
<service name ="MyServiceService " >
<port name ="MyServicePort " binding ="tns:MyServicePortBinding " >
<soap:address location ="http://localhost:8888/webService " />
</port>
</service>
</definitions>
四、创建一个空的客户端web项目
五、利用cmd命令生产客户端代码
wsimport -d "(客户端项目src路径:E:\tan\Java\Workspaces\TheClient\src)" -keep (接口调用url:http://localhost:8888/webService?wsdl)
六、创建客户端测试类测试
package webservice; public class MyClient { public static void main(String[] args) { MyServiceService myServiceService = new MyServiceService(); MyService myServicePort = myServiceService.getMyServicePort(); System.out.println(myServicePort.add(44, 570)); }
}
客户端输出:614
服务的输出:a+b=614