調用WebService實例,javaEE6 Feature之wsimport工具

     在javaEE6的bin文件夾時,有一個wsimport.exe,這個工具在5.0以前的版本里是沒有的,這個工具依據wsdl文件生成相應的類文件,而後用這些類文件,就能夠像調用本地的類同樣調用WebService提供的方法了javascript

 

  • wsimport工具詳細參數

The wsimport tool generates JAX-WS portable artifacts, such as:html

  • Service Endpoint Interface (SEI)
  • Service
  • Exception class mapped from wsdl:fault (if any)
  • Async Reponse Bean derived from response wsdl:message (if any)
  • JAXB generated value types (mapped java classes from schema types)

These artifacts can be packaged in a WAR file with the WSDL and schema documents along with the endpoint implementation to be deployed. The generated Service class can be used to invoke the Web Service endpoint.java

 

wsimport [options] <wsdl>web

 

Optionapp

Descriptionide

-d <directory>工具

Specify where to place generated output files測試

-b <path>this

Specify external JAX-WS or JAXB binding files (Each <file> must have its own -b)spa

-catalog
Specify catalog file to resolve external entity references, it supports TR9401, XCatalog, and OASIS XML Catalog format. Please read the XML Entity and URI Resolvers document or seewsimport_catalog sample.

-extension

allow vendor extensions (functionality not specified by the specification). Use of extensions may result in applications that are not portable or may not interoperate with other implementations

-help

Display help

-httpproxy:<host>:<port>

Specify an HTTP proxy server (port defaults to 8080)

-keep

Keep generated files

-p Specifying a target package via this command-line option, overrides any wsdl and schema binding customization for package name and the default package name algorithm defined in the specification

-s <directory>

Specify where to place generated source files

-verbose

Output messages about what the compiler is doing

-version

Print version information

-wsdllocation <location>
@WebService.wsdlLocation and @WebServiceClient.wsdlLocation value

 

  • wsimport工具用法實例

 

首先,須保證你的jdk爲6.0以上版本,能夠在command line下試運行這個命令

c:\test> wsimport

 

假設咱們有下面的wsdl文件

http://localhost:9080/WebService/TestService/TestService.wsdl

 

咱們要把生成的代碼放到c:/test/com.bjmaxinfo.rpc.merchantware.ws目錄下,那麼咱們運行如下命令便可

 

c:\test> wsimport -p com.bjmaxinfo.rpc.merchantware.ws -keep  http://localhost:9080/WebService/TestService/TestService.wsdl

 

而後,你就會看到在c:\test\generate目錄下生成了一些java代碼文件,wsimport就是如此簡單

 

 

  • 編寫調用WebService的客戶端

假如咱們要調用的WebService就是以前舉例提到的TestService,它只有下面這樣一個方法

 

Java代碼    收藏代碼
  1. public String test(String name) throws SOAPException  
  2. {  
  3.      if (name == null)  
  4.     {  
  5.         throw new SOAPException("name can't be null!");  
  6.     }  
  7.           
  8.     return "hello " + name;   
  9. }  

 

用wsimport工具,咱們能夠獲得以下代碼文件

 

generate

    |--com

        |--company

            |--ObjectFactory.java

            |--SOAPException.java

            |--SOAPException_Exception.java

            |--Test.java

            |--TestResponse.java

            |--TestService.java

            |--TestService_Service.java

            |--package-info.java

 

把它們編譯,而後寫下面這樣一個application去測試

 

 

Java代碼    收藏代碼
  1. public static void main(String[] args)  
  2. {  
  3.     TestService_Service serviceFactory = new TestService_Service();  
  4.     TestService service = serviceFactory.getTestServicePort();  
  5.     try  
  6.     {  
  7.         System.out.println(service.test(null));  
  8.     }  
  9.     catch (SOAPException_Exception ex)  
  10.     {  
  11.         System.out.println(ex.getMessage());  
  12.     }  
  13. }  

 

運行它,屏幕就會輸出

name can't be null

 

由於咱們以前傳了一個null值進去嘛,其實這也是爲了測試SOAPException是否正常運行,若是你傳個正確的字符串進去,webservice就能夠正常運行,如System.out.println(service.test("javaeye"));,屏幕上就會顯示,

Hello javaeye!

 

順便提一下WebService的異常處理,全部WebService的異常都必須用SOAPException拋出,它是java.xml.soap包中的一個類 (本人試過其它一些Exception,都不能被正常捕捉,只有SOAPException能夠正常工做,具體緣由不明,若是有哪一個人清楚這一點,請評論告之,謝謝)

相關文章
相關標籤/搜索