【问题标题】:need to use custom classes instead of generated (by wsimport) in web-services需要在 Web 服务中使用自定义类而不是生成(通过 wsimport)
【发布时间】:2010-06-02 18:04:12
【问题描述】:

您能帮忙解决以下问题吗?

当生成 WS 客户端代码(使用 wsimport ant 任务)时,所有类都会自动生成在与 Web 服务相同的包(例如 helloservice.endpoint)中,例如如果我的网络服务有方法

公共节点getNode();

所以生成了类 helloservice.endpoint.Node。 不过,我有自己的 helloservice.Node 类,我想在 web 服务中使用它。

我定义了 bind.xml 文件:


<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 任务,但得到错误:

 [wsimport] [ERROR] XPath 评估“wsdl:definitions/wsdl:portType[@name='Node']”导致空目标节点
 [wsimport] 文件第 2 行:/C:/work/projects/svn.ct/trunk/jwstutorial20/examples/jaxws/simpleclient/bind.xml

有人可以推荐这里有什么问题吗? 我可以以这种方式在生成的 Web 服务类中使用我自己的类,还是我需要更复杂的东西?

提前致谢。

【问题讨论】:

    标签: java web-services jaxb wsimport


    【解决方案1】:

    要从 wsdl 生成类,请在 ant 中使用:

    
    <taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport">
    <wsimport keep="true" sourcedestdir="..." wsdl="..." wsdllocation="..." xnocompile="true" />
    

    不要在 wsimport ant 任务上使用 'package' 属性,因此所有类都在正确的包中创建。

    一般来说,要自定义包,即将生成的包名 a.b.c 更改为名称 x.y.z 添加元素到 wsimport 任务并定义 binding.jxb 文件如下。

    
    <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-a.b.c.xsd 是 wsgen 任务生成的模式(使用合适的方案创建 wsdl)。

    有关 JAXB 自定义的更多详细信息:http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/tutorial/doc/JavaWSTutorial.pdf,“自定义 JAXB 绑定”部分

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多