创建JWS项目步骤:
1:创建接口
2:创建实现类
3:开启服务
1:编写接口
@WebService
public interface IMyService { public int add(int a,int b); public int minus(int a,int b);
}
1:编写接口实现类
@WebService(endpointInterface = "org.wnj.service.IMyService") //指定接口包名类包
public class MyServiceImpl implements IMyService { /** {@inheritDoc} */ @Override
public int add(int a, int b) {
System.out.println(a + "+" + b + "=" + (a + b));
return a + b;
} /** {@inheritDoc} */ @Override
public int minus(int a, int b) {
System.out.println(a + "-" + b + "=" + (a - b));
return a - b;
} }
//3:启动服务
public class MyServer { public static void main(String[] args) {
String address = "http://localhost:8888/ns";
Endpoint.publish(address, new MyServiceImpl());
}
}
//4:编写客户端
/** <一句话功能简述>
* <功能详细描述>
* @param args
*/
public static void main(String[] args) { try {
//创建访问wsdl服务的地址
URL url = new URL("http://localhost:8888/ns?wsdl");
//通过QName指明服务的具体信息
QName sname = new QName("http://service.wnj.org/","MyServiceImplService");
//创建服务
Service service = Service.create(url, sname);
//实现接口(最大的问题需要知道接口)
IMyService ms = service.getPort(IMyService.class);
System.out.println(ms.add(1, 2));
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
通过浏览器访问 : http://localhost:8888/ns?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://service.wnj.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.wnj.org/" name="MyServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://service.wnj.org/" schemaLocation="http://localhost:8888/ns?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>
<message name="minus">
<part name="parameters" element="tns:minus"/>
</message>
<message name="minusResponse">
<part name="parameters" element="tns:minusResponse"/>
</message>
<portType name="IMyService">
<operation name="add">
<input message="tns:add"/>
<output message="tns:addResponse"/>
</operation>
<operation name="minus">
<input message="tns:minus"/>
<output message="tns:minusResponse"/>
</operation>
</portType>
<binding name="MyServiceImplPortBinding" type="tns:IMyService">
<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>
<operation name="minus">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="MyServiceImplService">
<port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">
<soap:address location="http://localhost:8888/ns"/>
</port>
</service>
</definitions>
WSDL标签解释
type:用来定义的传输类型,用SOAP(Simple Object Access Protool协议指定参数
mesage:用来传输SOAP消息
portType:指定服务器的接口,并且通过operation绑定方法名及相应的in,out的消息,其中in表示入参,out表示出参
binding:指定传输消息所使用的格式
service:指定服务所发布的信息