1.Web Services 简介
Web Services, 即 "Web 服务", 简称 "WS", 其实就是"基于Web 服务" , 所谓的服务是双方的,有服务的提供方,也有服务的需求方, 通常是服务提供方发布服务,服务需求方调用服务
WebServices 其实就是建立在HTTP协议上实现异构系统通讯的工具, 数据都是通过HTTP进行传输的, 所谓的异构系统通讯是指你在广州用C#开发的应用可以调用我在上海开发用Java开发的应用, 通过调用Java 系统对外发布的Web Services,获取Java系统中的数据,在开始之前我简单说下Web Services中的术语:
WSDL: (Web Services Description Language) Web Services 描述语言
SOAP:(Simple Object Access Protocol) 简单对象访问协议
如果想知道更多Web Services的概念和术语, 可以参考: http://baike.baidu.com/view/67105.htm
2.使用JDK发布WS
第一步: 写一个服务接口
@WebService
public interface HelloServices {
String sayHello(String name);
}
在接口放一个@WebService注解, 说明该接口是一个Web Services接口
第二步: 实现WebService 接口, 在实现类中完成具体业务逻辑
@WebService(serviceName = "HelloService",portName = "HelloServicePort",endpointInterface = "com.HelloServices")
public class HelloServicesImpl implements HelloServices {
public String sayHello(String name) {
return "hello " + name;
}
}
第三步: 写一个Server类, 用于发布WebServices, 用JDK提供的工具即可实现
public class Server {
public static void main(String[] args) {
String address = "http://localhost:8080/ws/soap/hello";
HelloServices helloServices = new HelloServicesImpl();
Endpoint.publish(address, helloServices);
System.out.println("Web Services is published");
}
}
运行Server类中的main方法 会在控制台看到"Web Services is published" 的提示,说明Web Services 成功发布
第四步:打开浏览器,在地址栏中输入以下地址:
http://localhost:8080/ws/soap/hello?wsdl
注意:以上地址后面有一个 ?wsdl
后缀,在 Server
类中的 address 里却没有这个后缀。此时,在浏览器中会看到如下 XML 文档:
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<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://com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://com/"name="HelloService">
<types>
<xsd:schema>
<xsd:import namespace="http://com/" schemaLocation="http://localhost:8080/ws/soap/hello?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="HelloServices">
<operation name="sayHello">
<input wsam:Action="http://com/HelloServices/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://com/HelloServices/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="HelloServicePortBinding" type="tns:HelloServices">
<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="HelloService">
<port name="HelloServicePort" binding="tns:HelloServicePortBinding">
<soap:address location="http://localhost:8080/ws/soap/hello"/>
</port>
</service>
</definitions>
3.总结
通过前面的介绍, 我们知道可以使用 JDK发布Web Services, 那么为了让Web Services 的开发与使用变得更加简单, 更加轻量级,我们隆重介绍一下CXF, CXF的前身是XFire, 不仅用于开发基于SOAP(Simple Object Access Protocol)的Web Services,同时也适用于开发基于REST(Representational State Transfer) 的Web Services, 下一篇我将介绍一下CXF与spring的集成