【问题标题】:Wsdl2Java CXF - generating JAXBElement list instead of fieldsWsdl2Java CXF - 生成 JAXBElement 列表而不是字段
【发布时间】:2013-09-05 13:51:35
【问题描述】:

我的 xsd 文件中有以下条目:

<xsd:element name="isCaseCreationAllowedResponse">
    <xsd:complexType>
        <xsd:sequence>
            ....
            <xsd:element maxOccurs="1" minOccurs="0" name="operatorNote" type="xsd:string">
            </xsd:element>
            ....
            <xsd:sequence id="allowCaseWithNewContract">
                <xsd:element maxOccurs="1" minOccurs="1" name="allowCaseWithNewContract" type="xsd:boolean">
                </xsd:element>
                <xsd:choice minOccurs="0">
                    <xsd:element name="validationError" type="mnp:ErrorType"/>
                    <xsd:element name="internalError" type="mnp:ErrorType"/>
                    <xsd:element name="businessError" type="mnp:ErrorType"/>
                    <xsd:element name="externalError" type="mnp:ErrorType"/>
                </xsd:choice>
            </xsd:sequence>
            ....
            <xsd:sequence id="allowCaseWithExistingContract">
                <xsd:element maxOccurs="1" minOccurs="1" name="allowCaseWithExistingContract" type="xsd:boolean">
                </xsd:element>
                <xsd:choice minOccurs="0">
                    <xsd:element name="validationError" type="mnp:ErrorType"/>
                    <xsd:element name="internalError" type="mnp:ErrorType"/>
                    <xsd:element name="businessError" type="mnp:ErrorType"/>
                    <xsd:element name="externalError" type="mnp:ErrorType"/>
                </xsd:choice>
             </xsd:sequence>
             ....
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

因为按顺序生成的java文件是这样的:

public class OperationResponse {
    protected List<JAXBElement<?>> content;

    public List<JAXBElement<?>> getContent() {
        if (content == null) {
            content = new ArrayList<JAXBElement<?>>();
        }
        return this.content;
    } 
}

有没有办法配置CXF/JAXB/JAXWS在java类中生成字段,不是这个不直观的列表。

WSDL不能修改,所以只能配置wsdl2java。我在 jaxb 绑定中尝试了 xjc:simple,但它没有像我预期的那样工作。

我正在使用 CXF 2.7.5 和 maven cxf-codegen-plugin。

使用 xjc 我有以下注释

<xsd:schema>
    <xsd:annotation>
        <xsd:appinfo>
            <jaxb:globalBindings generateValueClass="false">
                <xjc:simple />
            </jaxb:globalBindings>
        </xsd:appinfo>
    </xsd:annotation>
</xsd:schema>

生成的类:

public class OperationResponse {
    protected String operatorNote;
    ....
    protected boolean allowCaseWithNewContract;
    @XmlElementRefs({
        @XmlElementRef for allowCaseWithExistingContract 
        @XmlElementRef for validationError and so on  
    })
    protected List<JAXBElement<?>>        validationErrorsAndAllowCaseWithExistingContractsAndInternalErrors;
}

具有上述配置的 Xjc 生成了另一个 JAXBelement 列表,这就是它没有按我预期工作的原因(删除所有 JAXBElement 列表并用包含适当字段的对象替换它们)。

编辑

使用以下配置:

<xsd:appinfo>
<jaxb:globalBindings generateElementProperty="false" fixedAttributeAsConstantProperty="true" choiceContentProperty="true">
</jaxb:globalBindings>
<jaxb:bindings>
    <jaxb:bindings node="//xsd:element[@name='isContractSigningAllowedResponse']/xsd:complexType/xsd:sequence/xsd:sequence[@id='allowStandardContract']">
        <jaxb:property name="allowStandardContractSequence"/>
    </jaxb:bindings>
    <jaxb:bindings node="//xsd:element[@name='isContractSigningAllowedResponse']/xsd:complexType/xsd:sequence/xsd:sequence[@id='allowOneVisitContract']">
        <jaxb:property name="allowOneVisitContractSequence" />
    </jaxb:bindings>
    <jaxb:bindings node="//xsd:element[@name='isCaseCreationAllowedResponse']/xsd:complexType/xsd:sequence/xsd:sequence[@id='allowCaseWithNewContract']">
        <jaxb:property name="allowCaseWithNewContractSequence"/>
    </jaxb:bindings>
    <jaxb:bindings node="//xsd:element[@name='isCaseCreationAllowedResponse']/xsd:complexType/xsd:sequence/xsd:sequence[@id='allowCaseWithExistingContract']">
        <jaxb:property name="allowCaseWithExistingContractSequence" />
    </jaxb:bindings>
</jaxb:bindings>
</xsd:appinfo>

我得到了从 wsdl2java 生成的类似的东西:

public class IsCaseCreationAllowedResponse {

    @XmlElement(required = true)
    protected String msisdn;
    protected String operator;
    protected String operatorNote;
    protected String routingNumber;
    @XmlElementRefs({
        @XmlElementRef(name = "internalError", namespace = "http://playmobile.pl/common/mnpm/schema", type = JAXBElement.class),
        @XmlElementRef(name = "externalError", namespace = "http://playmobile.pl/common/mnpm/schema", type = JAXBElement.class),
        @XmlElementRef(name = "validationError", namespace = "http://playmobile.pl/common/mnpm/schema", type = JAXBElement.class),
        @XmlElementRef(name = "businessError", namespace = "http://playmobile.pl/common/mnpm/schema", type = JAXBElement.class),
        @XmlElementRef(name = "allowCaseWithNewContract", namespace = "http://playmobile.pl/common/mnpm/schema", type = JAXBElement.class)
    })
    protected List<JAXBElement<?>> allowCaseWithNewContractSequence;
    @XmlElementRefs({
        @XmlElementRef(name = "validationError", namespace = "http://playmobile.pl/common/mnpm/schema", type = JAXBElement.class),
        @XmlElementRef(name = "externalError", namespace = "http://playmobile.pl/common/mnpm/schema", type = JAXBElement.class),
        @XmlElementRef(name = "businessError", namespace = "http://playmobile.pl/common/mnpm/schema", type = JAXBElement.class),
        @XmlElementRef(name = "internalError", namespace = "http://playmobile.pl/common/mnpm/schema", type = JAXBElement.class),
        @XmlElementRef(name = "allowCaseWithExistingContract", namespace = "http://playmobile.pl/common/mnpm/schema", type = JAXBElement.class)
    })
    protected List<JAXBElement<?>> allowCaseWithExistingContractSequence;
    protected String mnpmContractId;
    protected String contractNumber;
    protected String ccbsAccountId;
    protected Boolean fixedNumber;
    protected String description;
    @XmlElement(required = true)
    protected OperatorListType activeOperators;

}

我必须在内部序列中添加一些 id 才能正确绑定。

有什么方法可以将 allowCaseWithExistingContractSequence 和 allowCaseWithNewContractSequence 列表分解为一些具有适当字段的对象?

【问题讨论】:

  • 您能告诉我们为什么 xjc:simple 没有按您的预期工作吗?您不喜欢复数形式还是获得相同的源代码?
  • 由于评论长度限制,我回答了主要问题

标签: java web-services maven jaxb cxf


【解决方案1】:

我的绑定文件中有以下设置,并摆脱了 JAXBElements 并获得了不错的字段:)

&lt;jaxb:globalBindings generateElementProperty="false" fixedAttributeAsConstantProperty="true" choiceContentProperty="true"&gt;

如果您不想在 1 个字段中选择元素(...或...或...),请将其设置为 false。

【讨论】:

  • 如果将choiceContentProperty设置为false会发生什么?
  • 如何为 WSDL 执行此操作?
猜你喜欢
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 2015-12-18
  • 2012-09-12
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多