一、web Service是啥
它是一个远程方法调用(rpc)的一种实现。
借助于开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式下互相操作的应用程序。
它能够运行在不同机器上的不同应用上,而不需要借助附加的、专门的第三方软件或硬件,就可以互相交换数据或集成。可以跨语言、跨平台。
二、web Service模型
它的体系架构基于三种角色(服务提供者、服务注册中心、服务消费者)之间的交互。在典型情况下,服务提供者会将定义webService的服务描述并把它发布到服务注册中心;服务消费者使用查找操作来从服务注册中心检索服务描述,然后使用服务描述与服务提供者进行绑定并调用WebService实现和它的交互。
三、使用
1.服务端,定义服务:
//定义服务
@WebService
public class HelloService {
public void sayHello(String name){
System.out.println(name + "hello !");
}
}
public class Application {
public static void main(String[] args) {
//通过Endpoint发布服务。
Endpoint.publish("http://localhost:9001/service/sayHello", new HelloService());
System.out.println("发布服务成功");
}
}
这时可以通过浏览器访问http://localhost:9001/service/sayHello?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://service.server.ldd.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.server.ldd.com/" name="HelloServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://service.server.ldd.com/" schemaLocation="http://localhost:9001/service/sayHello?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="HelloService">
<operation name="sayHello">
<input wsam:Action="http://service.server.ldd.com/HelloService/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://service.server.ldd.com/HelloService/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="HelloServicePortBinding" type="tns:HelloService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloServiceService">
<port name="HelloServicePort" binding="tns:HelloServicePortBinding">
<soap:address location="http://localhost:9001/service/sayHello"/>
</port>
</service>
</definitions>
2.客户端
可以通过jdk内置的命令来完成。
为jdk/bin/wsimport.exe的命令。
格式:wsimport -s “src目录” -p “生成类所在包名” -keep “wsdl发布地址”
示例:
wsimport -s G:\workspace\webService\TheClient\src -p com.demo.client -keep http://localhost:9001/Service/ServiceHello?wsdl
当命令执行完毕时,会生成对应的java文件。
当出现以上java文件时,表示生成完毕。
public class Application {
public static void main(String[] args) {
System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
//-Dsun.misc.ProxyGenerator.saveGeneratedFiles=true
HelloServiceService service = new HelloServiceService();
HelloService hello = service.getHelloServicePort();
hello.sayHello("李华");
}
}
注意:若不存在该命令,则为jdk的版本太低了。