简体   繁体   English

为什么wsimport生成的类需要JAXBElement <ClassName>参数?

[英]Why classes generated by wsimport requires JAXBElement<ClassName> parameters?

I have a WSDL file which is from an Axis2 Web Service. 我有一个来自Axis2 Web服务的WSDL文件。 When I generate a client stub using wsimport given the WSDL file, the resulting classes require JAXBElement paramaters. 当我使用给定WSDL文件的wsimport生成客户端存根时,生成的类需要JAXBElement参数。 Why is it like that? 为什么会那样?

Sample Method from one of the Generated Classes: 来自其中一个生成类的示例方法:

JAXBElement<DataBean> value;

public void setValue(JAXBElement<DataBean> value)
{
    this.value = ((JAXBElement<DataBean>) value);
}

I am expecting it to look like this (without the JAXBElement): 我希望它看起来像这样(没有JAXBElement):

DataBean value;

public void setValue(DataBean value)
{
    this.value= (DataBean) value;
}

The tutorials I saw on the net does not set the classes to JAXBElement. 我在网上看到的教程没有将类设置为JAXBElement。 What could be the problem? 可能是什么问题呢? Please take note that the server is an Axis2 web service and the WSDL file is auto-generated by Axis2. 请注意,服务器是Axis2 Web服务,WSDL文件由Axis2自动生成。 The assumption is I have no control over the server. 假设是我无法控制服务器。

How can I make it in such a way that wsimport won't convert the parameters to JAXBElements? 如何以wsimport不将参数转换为JAXBElements的方式创建它?

Below is an excerpt from the WSDL file: (I ignored some of the tags to include only the essential tags) 以下是WSDL文件的摘录:(我忽略了一些标签只包含必要的标签)

<xs:element name="getData">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="0" name="getData" nillable="true" type="ax220:getData"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="getData">
    <xs:sequence>
        <xs:element minOccurs="0" name="value" nillable="true" type="ax219:DataBean"/>
    </xs:sequence>
</xs:complexType>

<wsdl:message name="getDataRequest">
    <wsdl:part name="parameters" element="ns:getData"/>
</wsdl:message>

<wsdl:message name="getDataResponse">
    <wsdl:part name="parameters" element="ns:getDataResponse"/>
</wsdl:message>

<wsdl:operation name="getData">
    <wsdl:input message="ns:getDataRequest" wsaw:Action="urn:getData"/>
    <wsdl:output message="ns:getDataResponse" wsaw:Action="urn:getDataResponse"/>
</wsdl:operation>

<wsdl:operation name="getData">
    <soap:operation soapAction="urn:getData" style="document"/>
    <wsdl:input>
        <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output>
        <soap:body use="literal"/>
    </wsdl:output>
</wsdl:operation>

<wsdl:operation name="getData">
    <soap12:operation soapAction="urn:getData" style="document"/>
    <wsdl:input>
        <soap12:body use="literal"/>
    </wsdl:input>
    <wsdl:output>
        <soap12:body use="literal"/>
    </wsdl:output>
</wsdl:operation>

<wsdl:operation name="getData">
    <http:operation location="getData"/>
    <wsdl:input>
        <mime:content type="text/xml" part="parameters"/>
    </wsdl:input>
    <wsdl:output>
        <mime:content type="text/xml" part="parameters"/>
    </wsdl:output>
</wsdl:operation>

As read on this page : 正如本页所述:

http://www.techdevtips.com/java/java-webservice-client-how-to-remove-jaxbelement http://www.techdevtips.com/java/java-webservice-client-how-to-remove-jaxbelement

use a data binding file with this code : 使用此代码的数据绑定文件:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
  xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc">
  <jaxb:globalBindings generateElementProperty="false">
    <xjc:simple />
  </jaxb:globalBindings>
</jaxb:bindings>

and use it in your wsimport ant task by filling the binding attribute (or -b flag argument if you use the runnable) 并通过填充绑定属性(或-b标志参数,如果您使用runnable)在wsimport ant任务中使用它

Cheers :) 干杯:)

To begin with: I don't think this can be done. 首先:我不认为这可以做到。 That is, I don't think you can tell wsimport to generate the classes differently. 也就是说,我认为你不能告诉wsimport以不同的方式生成类。 However, I can tell you how to modify the WSDL in a way that generates the schema differently and that could still enable you to talk to the service. 但是,我可以告诉您如何以不同方式生成模式的方式修改WSDL,这仍然可以使您与服务进行通信。

I took the type definitions from the WSDL, adjusted the name of the complexType and added the a type for DataBean that was missing above. 我从WSDL中获取了类型定义,调整了complexType的名称,并添加了上面缺少的DataBean类型。 I pasted that to a schema and compiled the schema with xjc, the JAXB schema compiler, which is used by wsimport to generate classes from the type defintions. 我将其粘贴到模式并使用xjc(JAXB模式编译器)编译模式,wsimport使用它来生成类型定义中的类。 Here is the schema: 这是架构:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/DataBean"
    xmlns:tns="http://www.example.org/DataBean" elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="getData">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="0" name="getDataType" type="tns:getDataType" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="getDataType">
        <xs:sequence>
            <xs:element minOccurs="0" name="value" type="tns:DataBean" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="DataBean">
        <xs:simpleContent>
            <xs:extension base="xs:int" />
        </xs:simpleContent>
    </xs:complexType>
</schema>

You don't need any special options for the compiler, simply execute xjc and point it to the schema file, then it will compile the source files. 您不需要编译器的任何特殊选项,只需执行xjc并将其指向模式文件,然后它将编译源文件。

The generated classes do not use JAXBElement as method parameters. 生成的类不使用JAXBElement作为方法参数。 That is they look like that: 那就是他们看起来像那样:

protected DataBean value;

public DataBean getValue() {
    return value;
}

Why is this? 为什么是这样? I removed the nillable="true" attributes from the element defintions and this did the trick. 我从元素定义中删除了nillable="true"属性,这就是诀窍。 nillable="true" states that explicit null values are legal here: nillable="true"表示显式空值在这里是合法的:

<DataBean></DataBean> 

If you remove this attribute, your code will run into problems if the service actually writes null values in there. 如果删除此属性,如果服务实际上在那里写入空值,则代码将遇到问题。 But after all the WSDL is generated and maybe Axis2 just thinks the nillable should be in there for some reason, although the implementation never actually uses it. 但是在生成所有WSDL之后,Axis2可能只是认为nillable应该出于某种原因,尽管实现从未实际使用它。 If you are lucky, it does not and everything will work fine, although you modified the WSDL. 如果你很幸运,它没有,一切都会正常工作,虽然你修改了WSDL。

I hope this helps! 我希望这有帮助! If not, then at least I have learned something today ;-) 如果没有,那么至少我今天学到了一些东西;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM