WebService wsdl wsimport

第一種使用JDK中的wsimport

在JDK的bin文件夾中,有一個wsimport.exe,這個工具依據wsdl文件生成相應的類文件,然後用這些類文件,就可以像調用本地的類一樣調用WebService提供的方法。


在命令提示符中使用如下命令:wsimport -keep -p com.demo.client http://localhost:8080/Demo/services/MyService?wsdl

命令參數說明:
  -d:生成客戶端執行類的class文件的存放目錄
  -s:生成客戶端執行類的源文件的存放目錄
  -p:定義生成類的包名

使用wsimport命令後,可以得到代碼,然後可以將代碼拷貝到工程裏面,也可以將其編譯,打包成jar。
比如  jar cvf dest.jar com/



第二種
使用第三方框架,以cxf爲例

    使用一下webservice的框架自帶的一下wsdl4j生成,由於衆所周知的原因,java的webservice的框架很多,經常由於版本和實現的不同,會出現一下莫名奇妙的問題,所以這個地方還是推薦使用你所使用的框架的wsdl4j工具生成相應的stub

 

  1.  下載apache-cxf-2.5.11.zip並解壓到D:\apache-cxf-2.5.11,下載地址:http://mirror.bjtu.edu.cn/apache/cxf/2.5.11/apache-cxf-2.5.11.tar.gz 
  2. 在D:\apache-cxf-2.5.11\bin目錄下面執行命令:wsdl2java  http://xxx.yyy.com:8102/ws/UserRemoteService?wsdl     
  3. 命令執行完成後在相關文件夾中會生成相應的代碼,將代碼全部引入接入系統,與wsimport是相同的

   使用過程中遇到的問題:

  •     在使用的過程中發現一個問題,由於jdk6最高只支持ws2.1規範版本,所以在jdk6下面生成的時候,要加上-frontend jaxws21

        wsdl2java -frontend jaxws21  http://xxx.yyy.com:8102/ws/UserRemoteService?wsdl    

 

  • 發現可以根據wsdl生成java類,java類中的註釋有可能是中文,所以要加上encoding參數,例如:-encoding utf-8  

       wsdl2java -ecoding utf-8  http://xxx.yyy.com:8102/ws/UserRemoteService?wsdl  


使用第三方ws框架,以cxf爲例:

   使用一下webservice的框架自帶的一下wsdl4j生成,由於衆所周知的原因,java的webservice的框架很多,經常由於版本和實現的不同,會出現一下莫名奇妙的問題,所以這個地方還是推薦使用你所使用的框架的wsdl4j工具生成相應的stub

 

  1.  下載apache-cxf-2.5.11.zip並解壓到D:\apache-cxf-2.5.11,下載地址:http://mirror.bjtu.edu.cn/apache/cxf/2.5.11/apache-cxf-2.5.11.tar.gz 
  2. 在D:\apache-cxf-2.5.11\bin目錄下面執行命令:wsdl2java  http://xxx.yyy.com:8102/ws/UserRemoteService?wsdl     
  3. 命令執行完成後在相關文件夾中會生成相應的代碼,將代碼全部引入接入系統,與wsimport是相同的

   使用過程中遇到的問題:

  •     在使用的過程中發現一個問題,由於jdk6最高只支持ws2.1規範版本,所以在jdk6下面生成的時候,要加上-frontend jaxws21

        wsdl2java -frontend jaxws21  http://xxx.yyy.com:8102/ws/UserRemoteService?wsdl    

 

  • 發現可以根據wsdl生成java類,java類中的註釋有可能是中文,所以要加上encoding參數,例如:-encoding utf-8  

       wsdl2java -ecoding utf-8  http://xxx.yyy.com:8102/ws/UserRemoteService?wsdl



WEBSERVICE快速入門的示例

首先定義接口:

package com.whaty.platform.ws.server;  
  
import javax.jws.WebService;  
  
/** 
 * @className:IMyservice.java 
 * @Desc:定義:SEI service endpoint interface 
 * @author:lizhuang 
 * @createTime:2012-12-21 上午12:57:18 
 */  
//JAX-WS註解,表示java api xml for webservice。JDK自帶API的XML格式的webservice  
@WebService  
public interface IMyservice {  
      
    int add(int a, int b);  
  
    int minus(int a, int b);  
}  


其次編寫實現類:

package com.whaty.platform.ws.server;  
  
import javax.jws.WebService;  
  
/**  
 * @className:MyServiceImpl.java 
 * @Desc:定義:SIB service implemention bean 
 * @author:lizhuang 
 * @createTime:2012-12-21 上午01:01:22 
 */  

//endpointInterface指定接入點接口:接口必須存在  
@WebService(endpointInterface="com.whaty.platform.ws.server.IMyservice")  
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;  
    }  
  
} 

最後發佈我們的服務,直接右鍵運行main方法,如果控制檯沒報錯,多半是發佈成功了,否則檢查你的代碼:


package com.whaty.platform.ws.server;  
  
import javax.xml.ws.Endpoint;  
  
/** 
 * @className:MyServer.java 
 * @Desc:發佈服務 
 * @author:lizhuang 
 * @createTime:2012-12-21 上午01:02:39 
 */  
public class MyServer {  
    public static void main(String[] args) {  
        //訪問方式:http://localhost:7777/tudou?wsdl  
        String address="http://localhost:7777/tudou";  
        Endpoint.publish(address, new MyServiceImpl());  
    }  
}  

瀏覽器地址欄輸入:訪問webservice看看是否發佈成功【地址後面加上"?wsdl"】:

http://localhost:7777/tudou?wsdl


瀏覽器顯示如下:

This XML file does not appear to have any style information associated with it. The document tree is shown below.  
<!-- 
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.  
-->  
<!-- 
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.  
-->  
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.ws.platform.whaty.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.ws.platform.whaty.com/" name="MyServiceImplService">  
<types>  
<xsd:schema>  
<xsd:import namespace="http://server.ws.platform.whaty.com/" schemaLocation="http://localhost:7777/tudou?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 message="tns:minus"/>  
<output message="tns:minusResponse"/>  
</operation>  
<operation name="add">  
<input message="tns:add"/>  
<output 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:7777/tudou"/>  
</port>  
</service>  
</definitions>  

下面我們創建客戶端訪問:

package com.whaty.platform.ws.client;  
  
import java.net.MalformedURLException;  
import java.net.URL;  
  
import javax.xml.namespace.QName;  
import javax.xml.ws.Service;  
  
import com.whaty.platform.ws.server.IMyservice;  
  
/** 
 * @className:MyClient.java 
 * @Desc:訪問發佈的服務 
 * @author:lizhuang 
 * @createTime:2012-12-21 上午01:23:57 
 */  
public class MyClient {  
    public static void main(String[] args) {  
  
        try {  
            //服務WSDL Document的地址  
            URL url = new URL("http://localhost:7777/tudou?wsdl");  
            //Qnameqname是qualified name 的簡寫  
            //2.構成:由名字空間(namespace)前綴(prefix)以及冒號(:),還有一個元素名稱構成  
            //由發佈的wsdl可知namespace爲http://server.ws.platform.whaty.com/,  
            QName qname=new QName("http://server.ws.platform.whaty.com/","MyServiceImplService");  
            Service service=Service.create(url, qname);  
            IMyservice ms=service.getPort(IMyservice.class);  
            ms.add(1, 4);  
            ms.minus(1, 4);  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        }  
    }  
}  

控制檯打印如下:

a+b=5
a-b=-3

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章