gpt4 book ai didi

java - Wsdl2Java CXF - 生成 JAXBElement 列表而不是字段

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:00 29 4
gpt4 key购买 nike

我的 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 绑定(bind)中尝试了 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 以进行正确的绑定(bind)。

有什么方法可以将 allowCaseWithExistingContractSequenceallowCaseWithNewContractSequence 列表分解为具有适当字段的对象?

最佳答案

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

<jaxb:globalBindings generateElementProperty="false" fixedAttributeAsConstantProperty="true" choiceContentProperty="true">

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

关于java - Wsdl2Java CXF - 生成 JAXBElement 列表而不是字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18573468/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com