在javaEE6的bin文件夹时,有一个wsimport.exe,这个工具在5.0以前的版本里是没有的,这个工具依据wsdl文件生成相应的类文件,而后用这些类文件,就能够像调用本地的类同样调用WebService提供的方法了javascript
The wsimport
tool generates JAX-WS portable artifacts, such as:html
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 |
---|---|
|
Specify where to place generated output files测试 |
|
Specify external JAX-WS or JAXB binding files (Each |
-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. |
|
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 |
|
Display help |
|
Specify an HTTP proxy server (port defaults to 8080) |
|
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 |
|
Specify where to place generated source files |
|
Output messages about what the compiler is doing |
|
Print version information |
-wsdllocation <location> |
@WebService.wsdlLocation and @WebServiceClient.wsdlLocation value |
首先,须保证你的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就是以前举例提到的TestService,它只有下面这样一个方法
用wsimport工具,咱们能够获得以下代码文件
generate
|--com
|--company
|--ObjectFactory.java
|--SOAPException.java
|--SOAPException_Exception.java
|--Test.java
|--TestResponse.java
|--TestService.java
|--TestService_Service.java
|--package-info.java
把它们编译,而后写下面这样一个application去测试
运行它,屏幕就会输出
name can't be null
由于咱们以前传了一个null值进去嘛,其实这也是为了测试SOAPException是否正常运行,若是你传个正确的字符串进去,webservice就能够正常运行,如System.out.println(service.test("javaeye"));,屏幕上就会显示,
Hello javaeye!
顺便提一下WebService的异常处理,全部WebService的异常都必须用SOAPException抛出,它是java.xml.soap包中的一个类 (本人试过其它一些Exception,都不能被正常捕捉,只有SOAPException能够正常工做,具体缘由不明,若是有哪一个人清楚这一点,请评论告之,谢谢)