需要使用自定义类而不是在Web服务中生成(通过wsimport) [英] need to use custom classes instead of generated (by wsimport) in web-services
问题描述
请帮助解决以下问题?
生成WS客户端代码(使用wsimport ant任务)时,所有类都在同一个包中自动生成(例如helloservice.endpoint)作为Web服务,例如如果我的网络服务有方法
When generate WS client code (with wsimport ant task), all classes are generated automatically in the same package (e.g. helloservice.endpoint) as web service, e.g. if my web-service has method
公共节点getNode();
public Node getNode();
所以类helloservice.endpoint.Node生成。
尽管如此,我有自己的helloservice.Node类,我想在web服务中使用。
so class helloservice.endpoint.Node is generated. Nevertheless, I have my own helloservice.Node class that I want to use in web-service.
我定义了bind.xml文件:
I defined bind.xml file :
<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" >
<bindings node="wsdl:definitions/wsdl:portType[@name='Node']">
<class name="helloservice.Node"/>
</bindings>
</bindings>
并将其作为绑定参数传递给wsimport任务,但得到错误:
and pass it to wsimport task as binding parameter, but get the error :
[wsimport] [ERROR] XPath evaluation of "wsdl:definitions/wsdl:portType[@name='Node']" results in empty target node
[wsimport] line 2 of file:/C:/work/projects/svn.ct/trunk/jwstutorial20/examples/jaxws/simpleclient/bind.xml
有人可以推荐这里有什么问题吗?
我可以用这种方式在生成的Web服务类中使用我自己的类,或者我需要更复杂的smth?
Could anybody, please, recommend what is wrong here? Can I use my own classes in generated web-service classes in such way, or I need smth more complicated?
提前致谢。
推荐答案
要从wsdl生成类,请在ant中使用:
To generate classes from wsdl, use in ant :
<taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport">
<wsimport keep="true" sourcedestdir="..." wsdl="..." wsdllocation="..." xnocompile="true" />
不要在wsimport ant任务上使用'package'属性,因此所有类都是以正确的方式创建的package。
Don't use 'package' attribute on wsimport ant task, so all classes are created in their correct packages.
一般来说,要自定义包,即更改生成的包名称abc以命名xyz将元素添加到wsimport任务并定义binding.jxb文件,如下所示。
In general, to customize package, i.e. change generated package name a.b.c to name x.y.z add element to wsimport task and define binding.jxb file as follows.
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="schema-for-a.b.c.xsd" node="/xs:schema">
<jxb:schemaBindings>
<jxb:package name="x.y.z" />
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
其中schema-for-abcxsd是由wsgen任务生成的模式(用适当的方案创建wsdl) )。
where schema-for-a.b.c.xsd is the schema generated by wsgen task (that creates wsdl with suitable schemes).
有关JAXB定制的更多详细信息: http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/tutorial/doc/JavaWSTutorial.pdf ,section 自定义JAXB绑定
More detailed about JAXB customization : http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/tutorial/doc/JavaWSTutorial.pdf, section "Customizing JAXB Bindings"
这篇关于需要使用自定义类而不是在Web服务中生成(通过wsimport)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!