java中使用JDK發布WS


1、服務的發布

第一步:寫一個服務接口。

import javax.jws.WebService;

@WebService
public interface HelloService {

String say(String name);
}

第二步:實現這WS接口,在實現類中完成具體業務邏輯,如下:

import javax.jws.WebService;

@WebService(
serviceName="HelloService",
portName="HelloServicePort",
endpointInterface="cn.caixw.demo.webservice.HelloService")
public class HelloServiceImpl implements HelloService {

@Override
public String say(String name) {
// TODO Auto-generated method stub
return "hello"+name;
}

}

第三步:寫一個Server類,用於發布WS,直接使用JDK提供的工具即可實現。

public class WebServer {
public static void main(String[] args) {
String address="http://localhost:8080/ws/soap/hello";
HelloService helloService=new HelloServiceImpl();
Endpoint.publish(address, helloService);
System.out.println("service published");
}}

只需要使用JDK提供的javax.xml.ws.Endpoint即可發布WS,只需要提供一個WS

的地址,還需要提供一個服務實例。

第四步:打開瀏覽器,在地址欄中輸入一下地址:

http://localhost:8080/ws/soap/hello?wsdl

在瀏覽器中看到如下代碼:

<!--
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://webservice.demo.caixw.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.demo.caixw.cn/" name="HelloService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservice.demo.caixw.cn/" schemaLocation="http://localhost:8080/ws/soap/hello?xsd=1"/>
</xsd:schema>
</types>
<message name="say">
<part name="parameters" element="tns:say"/>
</message>
<message name="sayResponse">
<part name="parameters" element="tns:sayResponse"/>
</message>
<portType name="HelloService">
<operation name="say">
<input wsam:Action="http://webservice.demo.caixw.cn/HelloService/sayRequest" message="tns:say"/>
<output wsam:Action="http://webservice.demo.caixw.cn/HelloService/sayResponse" message="tns:sayResponse"/>
</operation>
</portType>
<binding name="HelloServicePortBinding" type="tns:HelloService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="say">
<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>
當看到這份文檔的時候,意味着,發布的WSDL服務可以被別人使用了。
 

 2、通過客戶端調用WS

第一步:使用JDK提供的命令行生成WS客戶端jar包。

在jdk的安裝目錄下有個bin目錄,里面存放了大量的命令行工具,其中,有一個名為wsimport的命令行工具,正是用來通過WSDL生成WS客戶端代碼,

只要輸入如下命令行即可:

wsimport http://xxxxxxxx?wsdl   ------>通過wsdl地址生成class文件 到當前目錄

jar -cf  client.jar .    ------> 將當前目錄生成jar包

mdir .           -------->刪除當前目錄內容

第二步:寫一個Client類,用於調用WS,需要使用上一步生成的WS客戶端jar包。

將生成的client.jar導入到項目的classpath中,客戶端代碼如下:

public static void main(String[] args) {


HelloService_Service helloFactory=new HelloService_Service(); ----->類似於服務工廠。
HelloService helloService1=helloFactory.getHelloServicePort();  ----->獲取服務
System.out.println(helloService1.say("bangbang"));   ------>調用服務


}

這是一個典型的“代理模式”應用場景,我們實際上是通過面向代理對象來調用WS的,並且這是一種“靜態代理”,

其實JDK已經具備了動態代理的功能,對於WS而言,JDK同樣也提供了很好的工具,就像下面這段代碼那樣:

try {
URL wsdl=new URL("http://localhost:8080/ws/soap/hello?wsdl");
QName serviceName=new QName("http://webservice.demo.caixw.cn/","HelloService");
QName portName=new QName("http://webservice.demo.caixw.cn/","HelloServicePort");

Service service=Service.create(wsdl, serviceName);
HelloService helloService=service.getPort(portName, HelloService.class);
System.out.println(helloService.say("fack"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

此時,只需要在本地提供一個HelloService的接口,無需client.jar,直接面向WSDL編程,只不過需要定義serviceName和portName這2個東西,

最后才能調用JDK提供的javax.xml.ws.Service類生成service對象,它同樣是一個工廠對象,通過該工廠對象獲取我們需要的HelloService實例。

貌似這樣方式也不是特別動態,畢竟HelloService接口還是需要自行提供的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM