非常感谢孙浩老师。
先创建wsdl:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.example.org/mywsdl/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
name="MyServiceImplService"
targetNamespace="http://www.example.org/mywsdl/">
<wsdl:types>
<xsd:schema targetNamespace="http://www.example.org/mywsdl/">
<xsd:element name="add" type="tns:addType" />
<xsd:element name="addResponse" type="tns:addResponseType" />
<xsd:element name="minus" type="tns:minusType" />
<xsd:element name="minusResponse" type="tns:minusResponseType" />
<xsd:complexType name="addType">
<xsd:sequence>
<xsd:element name="a" type="xsd:int" />
<xsd:element name="b" type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="addResponseType">
<xsd:sequence>
<xsd:element name="addResult" type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="minusType">
<xsd:sequence>
<xsd:element name="num1" type="xsd:int" />
<xsd:element name="num2" type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="minusResponseType">
<xsd:sequence>
<xsd:element name="minusResult" type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="add">
<wsdl:part element="tns:add" name="add" />
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part element="tns:addResponse" name="addResponse" />
</wsdl:message>
<wsdl:message name="minus">
<wsdl:part element="tns:minus" name="minus" />
</wsdl:message>
<wsdl:message name="minusResponse">
<wsdl:part element="tns:minusResponse" name="minusResponse" />
</wsdl:message>
<wsdl:portType name="IMyService">
<wsdl:operation name="add">
<wsdl:input message="tns:add" />
<wsdl:output message="tns:addResponse" />
</wsdl:operation>
<wsdl:operation name="minus">
<wsdl:input message="tns:minus" />
<wsdl:output message="tns:minusResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="myServiceSOAP" type="tns:IMyService">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="add">
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="minus">
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="MyServiceImplService">
<wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort">
<soap:address location="http://localhost:8888/ms" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
使用wsimport,得到服务端的Java文件,就拷贝IMyService.java到服务器项目中。
实现IMyService的实现类:
package org.example.mywsdl;
import javax.jws.WebService;
@WebService(endpointInterface = "org.example.mywsdl.IMyService", targetNamespace = "http://www.example.org/mywsdl/", wsdlLocation = "META-INF/wsdl/mywsdl.wsdl")
public class MyServiceImpl implements IMyService {
public MyServiceImpl() {
}
@Override
public int add(int a, int b) {
System.out.println(a + "+" + b + "=" + (a + b));
return (a + b);
}
@Override
public int minus(int num1, int num2) {
System.out.println(num1 + "-" + num2 + "=" + (num1 - num2));
return (num1 - num2);
}
}
package org.example.mywsdl;
import javax.xml.ws.Endpoint;
public class MyService {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8888/ms", new MyServiceImpl());
}
}
最终服务端项目的构成:
在使用wsimport得到客户端Java文件,把所有Java文件拷贝到客户端的项目中。
在运行测试类:
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8888/ms?wsdl");
QName name = new QName("http://www.example.org/mywsdl/","MyServiceImplService");
MyServiceImplService implService = new MyServiceImplService(url,
name);
IMyService ms = implService.getMyServiceImplPort();
System.out.println(ms.minus(33, 21));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
客户端控制台:
服务端控制台: