JDK-Tools-wsimport
wsimport
是JDK自带的可以根据WSDL文档生成客户端调用代码的工具。无论服务器端WebService使用什么语言编写的,都将在客户端生成Java代码。所以服务器用什么语言编写的并不重要。
wsimport常用参数
wsimport命令语法:wsimport [options] <WSDL_URI>
注意:WSDL_URI必须含有末尾的?wsdl
常用参数:
-d <directory>
,在指定的目录生成class文件-s <directory>
,在指定目录生成Java源文件-p <package>
,指定生成代码的包目录结构,会在生成的java代码中生成包声明语句package <package>
。-p指定的包目录会接在-s目录后面,即最终客户端代码生成的位置是:-s目录/-p目录。建议-s目录为工程src目录,-p指定完全包名,这样可以保证代码中的package包声明与实际目录一致。-keep
,在生成class文件,或者jar包时,同时保留java源文件-clientjar <jarfile>
,在当前目录生成jar文件,结合-d <directory>
可以在指定的目录生成jar文件
wsimport常用参数组合
在当前目录生成指定包结构的class文件和java源文件
wsimport -s . -p com.masikkk.jws.client http://localhost:8899/HelloService?wsdl
等价于:
wsimport -keep -p com.masikkk.jws.client http://localhost:8899/HelloService?wsdl
会在当前目录下生成子目录com/masikkk/jws/client/,其中有class文件和java源文件
在当前目录生成指定包结构的class文件,注意不带
-s
参数不会生成java源文件,只有class文件wsimport -p com.masikkk.jws.client http://localhost:8899/HelloService?wsdl
在指定目录生成指定包结构的class文件和java源文件
假设wsdl文档的uri为http://localhost:6666/service/interpret?wsdl ,那么在F:\temp下,生成包结构为cn.ljl.sand.jws.chapter3.client.wsimport的java源文件的命令为:wsimport -s F:\temp -p cn.ljl.sand.jws.chapter3.client.wsimport http://localhost:6666/service/interpret?wsdl
在指定目录生成指定包结构的jar文件
假设wsdl文档的uri为http://localhost:6666/service/interpret?wsdl ,那么在F:\temp下,生成包结构为cn.ljl.sand.jws.chapter3.client.wsimport的interpret-wsimport.jar的命令为:wsimport -d F:\temp -clientjar interpret-wsimport.jar -p cn.ljl.sand.jws.chapter3.client.wsimport http://localhost:6666/service/interpret?wsdl
客户端代码核心类
根据wsdl中定义的方法个数、参数类型等不同,wsimport自动生成的java源文件个数也不同,
一般来说,每个暴露的方法会对应生成2个java类,一个定义参数、一个定义返回值。
比如,HelloService包含3个暴露的方法sayHello,sayHelloName和sayHelloList,wsimport生成的HelloService的客户端代码为:
此外,如果方法的参数或返回值中有类对象,wsimport会自动生成对应参数类或返回值类的java代码。
比如,StudentService的参数为studentRequest类,返回值为StudentResponse类,wsimport生成的StudentService的客户端代码为:
wsimport生成的java源码中,有两个是我们需要直接用到的:
- 一个是以wsdl文档的service元素的name为名的类,HelloService中是HelloServiceImplService,通过HelloServiceImplService类的getHelloServiceImplPort()方法可以获得HelloServiceImpl类的实例,然后就可以像执行本地代码一样访问HelloServiceImpl类中的方法了。
- 一个是以wsdl文档的portType元素的name为名的接口,本例中是HelloServiceImpl,是一个和webservice发布类同名的接口,里面包含所有发布的方法,作为调用HelloServiceImpl类的方法的本地代理。
附:HelloService的wsdl文件
<!--
Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<!--
Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<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://simple.server.jws.masikkk.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://simple.server.jws.masikkk.com/" name="HelloServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://simple.server.jws.masikkk.com/" schemaLocation="http://localhost:8899/HelloService?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<message name="sayHelloName">
<part name="parameters" element="tns:sayHelloName"/>
</message>
<message name="sayHelloNameResponse">
<part name="parameters" element="tns:sayHelloNameResponse"/>
</message>
<message name="sayHelloList">
<part name="parameters" element="tns:sayHelloList"/>
</message>
<message name="sayHelloListResponse">
<part name="parameters" element="tns:sayHelloListResponse"/>
</message>
<portType name="HelloServiceImpl">
<operation name="sayHello">
<input wsam:Action="http://simple.server.jws.masikkk.com/HelloServiceImpl/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://simple.server.jws.masikkk.com/HelloServiceImpl/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
<operation name="sayHelloName">
<input wsam:Action="http://simple.server.jws.masikkk.com/HelloServiceImpl/sayHelloNameRequest" message="tns:sayHelloName"/>
<output wsam:Action="http://simple.server.jws.masikkk.com/HelloServiceImpl/sayHelloNameResponse" message="tns:sayHelloNameResponse"/>
</operation>
<operation name="sayHelloList">
<input wsam:Action="http://simple.server.jws.masikkk.com/HelloServiceImpl/sayHelloListRequest" message="tns:sayHelloList"/>
<output wsam:Action="http://simple.server.jws.masikkk.com/HelloServiceImpl/sayHelloListResponse" message="tns:sayHelloListResponse"/>
</operation>
</portType>
<binding name="HelloServiceImplPortBinding" type="tns:HelloServiceImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="sayHelloName">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="sayHelloList">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloServiceImplService">
<port name="HelloServiceImplPort" binding="tns:HelloServiceImplPortBinding">
<soap:address location="http://localhost:8899/HelloService"/>
</port>
</service>
</definitions>
附:StudentService的wsdl文件
<!--
Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<!--
Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<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://bean.server.jws.masikkk.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://bean.server.jws.masikkk.com/" name="StudentServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://bean.server.jws.masikkk.com/" schemaLocation="http://localhost:8899/StudentService?xsd=1"/>
</xsd:schema>
</types>
<message name="queryStudentResponse">
<part name="parameters" element="tns:queryStudentResponse"/>
</message>
<message name="queryStudentResponseResponse">
<part name="parameters" element="tns:queryStudentResponseResponse"/>
</message>
<message name="queryStudentResponseList">
<part name="parameters" element="tns:queryStudentResponseList"/>
</message>
<message name="queryStudentResponseListResponse">
<part name="parameters" element="tns:queryStudentResponseListResponse"/>
</message>
<portType name="StudentServiceImpl">
<operation name="queryStudentResponse">
<input wsam:Action="http://bean.server.jws.masikkk.com/StudentServiceImpl/queryStudentResponseRequest" message="tns:queryStudentResponse"/>
<output wsam:Action="http://bean.server.jws.masikkk.com/StudentServiceImpl/queryStudentResponseResponse" message="tns:queryStudentResponseResponse"/>
</operation>
<operation name="queryStudentResponseList">
<input wsam:Action="http://bean.server.jws.masikkk.com/StudentServiceImpl/queryStudentResponseListRequest" message="tns:queryStudentResponseList"/>
<output wsam:Action="http://bean.server.jws.masikkk.com/StudentServiceImpl/queryStudentResponseListResponse" message="tns:queryStudentResponseListResponse"/>
</operation>
</portType>
<binding name="StudentServiceImplPortBinding" type="tns:StudentServiceImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="queryStudentResponse">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="queryStudentResponseList">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="StudentServiceImplService">
<port name="StudentServiceImplPort" binding="tns:StudentServiceImplPortBinding">
<soap:address location="http://localhost:8899/StudentService"/>
</port>
</service>
</definitions>
遇到的问题
http://localhost:8080/helloService 服务发布成功,浏览器能访问到服务也可以访问到wsdl文件,使用wsimport命令生成客户端代码的时候出错。
C:\Users\MaSi>wsimport http://127.0.0.1:8080/helloService?wsdl
[ERROR] Server returned HTTP response code: 503 for URL: http://127.0.0.1:8080/helloService?wsdl
Failed to read the WSDL document: http://127.0.0.1:8080/helloService?wsdl, because 1) could not find the document; /2) the document could not be read; 3) the root element of the document is not <wsdl:definitions>.
[ERROR] failed.noservice=Could not find wsdl:service in the provided WSDL(s):
At least one WSDL with at least one service definition needs to be provided.
Failed to parse the WSDL.
原因:
1、是否设置了网络代理,如果设置了,去掉代理;
2、使用本机真实IP而不是127.0.0.1或localhost来发布webservice,例如:Endpoint.publish("http://192.168.1.123:8080/MyWebService", new TestWs());
用了两种方法都可以解决:
1、关闭公司网络代理,或将localhost加入代理“例外”中。
2、Endpoint.publish时地址修改为本机真实ip,wsimport成功。
参考
JAVA项目中发布WebService服务——简单实例
http://blog.csdn.net/hanxuemin12345/article/details/40163757Java WebService 简单实例
http://www.cnblogs.com/yisheng163/p/4524808.html基于wsimport生成代码的客户端
http://www.cnblogs.com/ywjy/p/5196064.html
上一篇 Hexo博客(16)添加百度推荐
下一篇 Java-JWS
页面信息
location:
protocol
: host
: hostname
: origin
: pathname
: href
: document:
referrer
: navigator:
platform
: userAgent
: