現在我們以前來看下,如何利用java API給我們提供的WebService接口來開發一個簡單的小案例。
1、首先我們定義一個接口:注意要引入Webservice的注解表示這是一個webservice的接口。
package com.lyl.service;
import javax.jws.WebService;
@WebService
public interface IMyService {
public int add(int a,int b);
public int minus(int a,int b);
}
2、然後定義個實作類:要添加@WebService(endpointInter)
表示要實作的接口。
package com.lyl.service;
import javax.jws.WebService;
@WebService(endpointInter)
public class MyServiceImpl implements IMyService {
public int add(int a, int b) {
System.out.println(a+"+"+b+"="+(a+b));
return a+b;
}
public int minus(int a, int b) {
System.out.println(a+"-"+b+"="+(a-b));
return a-b;
}
}
3、在定義一個釋出服務的類:用于釋出在網上的路徑。
package com.lyl.service;
import javax.xml.ws.Endpoint;
public class MyServer {
public static void main(String[] args) {
String address="http://localhost:8080/ns";
Endpoint.publish(address,new MyServiceImpl());
}
}
這樣我們的一個WebService就已經定義好了,我們可以再浏覽器中輸入服務類中的釋出的位址:http://localhost:8080/ns即可通路了,這時可以看到如許頁面:
5、點選上面的超連結,就可看到自動生成的WSDL檔案了如:
<?xml version="1.0" encoding="UTF-8" ?>
- <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.2 in JDK 7.
-->
- <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.2 in JDK 7.
-->
- <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.lyl.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.lyl.com/" name="MyServiceImplService">
- <types>
- <xsd:schema>
<xsd:import namespace="http://service.lyl.com/" schemaLocation="http://localhost:8080/ns?xsd=1" />
</xsd:schema>
</types>
- <message name="minus">
<part name="parameters" element="tns:minus" />
</message>
- <message name="minusResponse">
<part name="parameters" element="tns:minusResponse" />
</message>
- <message name="add">
<part name="parameters" element="tns:add" />
</message>
- <message name="addResponse">
<part name="parameters" element="tns:addResponse" />
</message>
- <portType name="IMyService">
- <operation name="minus">
<input wsam:Action="http://service.lyl.com/IMyService/minusRequest" message="tns:minus" />
<output wsam:Action="http://service.lyl.com/IMyService/minusResponse" message="tns:minusResponse" />
</operation>
- <operation name="add">
<input wsam:Action="http://service.lyl.com/IMyService/addRequest" message="tns:add" />
<output wsam:Action="http://service.lyl.com/IMyService/addResponse" message="tns:addResponse" />
</operation>
</portType>
- <binding name="MyServiceImplPortBinding" type="tns:IMyService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="minus">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
- <operation name="add">
<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:8080/ns" />
</port>
</service>
</definitions>
6、 接下來我們通過JDK中自帶的指令wsimport指令(在JDK的安裝目錄bin檔案夾下的wsimpor.exe,如果配置了JDK的環境變量的話,可以再任以目錄下執行此指令,否則要轉到bin目錄下才能用該指令)将wsdl檔案導入到本地,生成用戶端代碼:
其中參數-keep:儲存源碼也就是java檔案-d 指定生成源代碼存放的路徑(如上是D://01檔案夾下) -verbose意思是顯示詳細資訊 http://localhost:8080/ns?wsdl指定的wsdl的網上路徑,如果是在本來的話可以使本地路徑。
7、再建立一個java Application項目,在src包下,添加生成的用戶端代碼,并添加一個測試類TestClient
如
8、添加一個測試類:
package com.lyl.test;
import com.lyl.service.IMyService;
import com.lyl.service.MyServiceImplService;
public class TestClient {
public static void main(String[] args) {
MyServiceImplService mis=new MyServiceImplService();
IMyService myservice=mis.getMyServiceImplPort();
myservice.add(4, 6);
}
}
9、執行後即可看結果(服務不要關閉):
好了,一個簡單的基于JAX-WS的webservice和用戶端和服務端就建好了,有興趣的話可以試一試。對了jdk最好是1.7的,要不然會有一點小錯誤,相信你能解決的 。