本文介绍了如何使用xsdtojava防止xs的JAXBElement自动生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 cxf-xjc-plugin XSD 生成java类xsdtojava 插件。

I'm generating java classes from XSD using cxf-xjc-plugin and xsdtojava plugin.

问题:我无法控制 xsd 和一个element有 xs:choice ,这会产生 JAXBElement 。不幸的是我的xml序列化器 jackson 无法处理 JAXBElement s。所以我试图实现自动生成到对象而不是 JAXBElement 。但是如何?

Problem: I have no control of the xsd, and one element has a xs:choice which results in a JAXBElement. Unfortunately my xml serializer jackson cannot handle JAXBElements. So I'm trying to achieve autogeneration to an Object rather than JAXBElement. But how?

xsd我无法控制:

<xs:element name="payment">
    <xs:complexType>
        <xs:choice maxOccurs="2">
            <xs:element name="creditcard">
                ...
            </xs:element>
            <xs:element name="debitcard">
                ...
            </xs:element>
            <xs:element name="iban">
                ...
            </xs:element>
        </xs:choice>
    </xs:complexType>
</xs:element>

xsdtojava生成:

xsdtojava generates:

@XmlRootElement
public class AutogeneratedReq {
    @XmlElementRefs({
         @XmlElementRef(name = "creditcard", type = JAXBElement.class, required = false),
         @XmlElementRef(name = "debitcard", type = JAXBElement.class, required = false),
         @XmlElementRef(name = "iban", type = JAXBElement.class, required = false)
    })
    private List<JAXBElement<?>> payment;
}

但我的目标如下:

    @XmlElements({
        @XmlElement(name="creditcard", type=Creditcad.class, required = false),
        @XmlElement(name="debitcard", type=Debitcard.class, required = false),
        @XmlElement(name="iban", type=Iban.class, required = false)
    })
    protected List<Object> payment;

或者也可以将每个选项生成为单个元素:

Or as well it would be ok generating each of the choices as single elements:

private List<Ceditcard> creditcard;
private List<Debitcard> debitcard;
private List<Iban> iban;

我尝试使用绑定文件实现此目的:

I tried achieving this by using a binding file:

<?xml version="1.0" encoding="UTF-8"?> 
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          version="2.1"> 
    <globalBindings choiceContentProperty="true"/> 
</bindings> 

但这没有帮助。我可以尝试更多什么?

But that did not help. What could I try more?

推荐答案

解决方法是使用简化 jaxb插件并使用生成选择元素<简化:as-element-property />

Solution is to use the simplify jaxb plugin and generating the choice elements with <simplify:as-element-property/>.

maven :

<dependency>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics</artifactId>
</dependency>

xsdtojava maven config with cxf-xjc-plugin 必须使用:

xsdtojava maven config with cxf-xjc-plugin must use:

<xsdOption>
   <bindingFile>${project.basedir}/src/main/resources/choice-binding.xml</bindingFile>
   <extensionArgs>
     <extensionArg>-Xsimplify</extensionArg>
   </extensionArgs>
</xsdOption>

choice-binding.xml:

choice-binding.xml:



axb:bindings    
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/simplify"
    jaxb:extensionBindingPrefixes="xjc simplify"
    jaxb:version="2.1">
    <jaxb:bindings schemaLocation="xsd/booking/bookingRequest.xsd">
                <jaxb:bindings multiple="true" node="//xs:element[@name='payment']//xs:complexType//xs:choice//xs:element">
                        <simplify:as-element-property/>
                </jaxb:bindings>
    </jaxb:binding>
</jaxb:bindings>

这篇关于如何使用xsdtojava防止xs的JAXBElement自动生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 20:03