什么是Web Services:
Web Services 是应用程序组件,使用开放协议进行通信,是独立的(self-contained)并可自我描述的,可通过使用UDDI来发现,可被其他应用程序使用,XML 是 Web Services 的基础。
基础的 Web Services 平台是 XML + HTTP。
HTTP 协议是最常用的因特网协议。
XML 提供了一种可用于不同的平台和编程语言之间的语言。
Web services 平台的元素:
SOAP (简易对象访问协议)
UDDI (通用描述、发现及整合)
WSDL (Web services 描述语言)
SOAP 是一种使应用程序有能力通过 HTTP 交换信息的基于 XML 的简易协议。
或者可以更简单地说:SOAP 是一种用于访问 web service 的协议。
WSDL 是基于 XML 的用来描述 Web services 以及如何访问它们的一种语言。
WSDL 可描述 web service,连同用于 web service 的消息格式和协议的细节。
创建服务端,使用Endpoint来发布服务:
//要发布的服务接口
public interface HelloService {
String sayHello(String name);
}
@WebService
public class HelloServiceImpl implements HelloService {
@WebMethod
public String sayHello(String name) {
return "Hello," + name +"java是世界上最好的语言";
}
}
public class App {
public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1:2121/web_service", new HelloServiceImpl());
System.out.println("webService发布成功........");
}
}
根据发布的URL在后面加上**?wsdl**在浏览器中输入 http://127.0.0.1:2121/web_service?wsdl
<!--
Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<!--
Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<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://impl.api.demo.aloys.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.api.demo.aloys.com/" name="HelloServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://impl.api.demo.aloys.com/" schemaLocation="http://127.0.0.1:2121/web_service?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="HelloServiceImpl">
<operation name="sayHello">
<input wsam:Action="http://impl.api.demo.aloys.com/HelloServiceImpl/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://impl.api.demo.aloys.com/HelloServiceImpl/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="HelloServiceImplPortBinding" type="tns:HelloServiceImpl">
<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="HelloServiceImplService">
<port name="HelloServiceImplPort" binding="tns:HelloServiceImplPortBinding">
<soap:address location="http://127.0.0.1:2121/web_service"/>
</port>
</service>
</definitions>
创建一个客户端(一个普通的java项目即可):
打开cmd输入命令:
wsimport -s 客户端项目的src路径 -keep http://127.0.0.1:2121/web_service?wsdl
刷新客户端项目,会有对应的WebService生成的文件,然后调用即可消费服务:
public class App {
public static void main(String[] args) {
HelloServiceImpl service = new HelloServiceImplService().getHelloServiceImplPort();
System.out.println(service.sayHello("jack "));
}
}