使用xsd:choice和xsd:sequence的jaxb问题

时间:2013-05-08 18:55:41

标签: json jaxb

我的架构有元素

    <xs:element name="field">  
        <xs:complexType>  
            <xs:sequence>  
                <xs:element ref="type" minOccurs="1" maxOccurs="1"/>  
                <xs:choice>  
                    <xs:sequence>  
                        <xs:choice>  
                            <xs:element ref="content"/>  
                            <xs:sequence>  
                                <xs:element ref="cmd"/>  
                                <xs:element ref="legend"/>  
                            </xs:sequence>  
                        </xs:choice>  
                        <xs:element ref="id" minOccurs="0"/>  
                    </xs:sequence>  
                    <xs:sequence>  
                        <xs:element ref="name"/>  
                        <xs:choice>  
                            <xs:sequence>  
                                <xs:element ref="value"/>  
                                <xs:element ref="label"/>  
                                <xs:element ref="optional"/>  
                            </xs:sequence>  
                            <xs:sequence>  
                                <xs:element ref="optional"/>  
                                <xs:choice>  
                                    <xs:sequence>  
                                        <xs:element ref="options"/>  
                                        <xs:element ref="id" minOccurs="0"/>  
                                    </xs:sequence>  
                                    <xs:sequence>  
                                        <xs:element ref="label"/>  
                                        <xs:element ref="options"/>  
                                    </xs:sequence>  
                                </xs:choice>  
                            </xs:sequence>  
                            <xs:sequence>  
                                <xs:element ref="label"/>  
                                <xs:element ref="optional"/>  
                                <xs:choice minOccurs="0">  
                                    <xs:element ref="boxes"/>  
                                    <xs:element ref="id"/>  
                                    <xs:element ref="options"/>  
                                </xs:choice>  
                            </xs:sequence>  
                        </xs:choice>  
                    </xs:sequence>  
                    <xs:sequence>  
                        <xs:element ref="id"/>  
                        <xs:element ref="name"/>  
                        <xs:element ref="label"/>  
                        <xs:element ref="optional"/>  
                    </xs:sequence>  
                </xs:choice>  
            </xs:sequence>  
        </xs:complexType>  
    </xs:element>  

这会生成类结构

@XmlAccessorType(XmlAccessType.FIELD)  
@XmlType(name = "", propOrder = {  
    "content"  
})  
@XmlRootElement(name = "field")  
public class Field {  

    @XmlElementRefs({  
        @XmlElementRef(name = "legend", type = Legend.class),  
        @XmlElementRef(name = "id", type = JAXBElement.class),  
        @XmlElementRef(name = "name", type = JAXBElement.class),  
        @XmlElementRef(name = "cmd", type = JAXBElement.class),  
        @XmlElementRef(name = "options", type = Options.class),  
        @XmlElementRef(name = "content", type = Content.class),  
        @XmlElementRef(name = "type", type = JAXBElement.class),  
        @XmlElementRef(name = "label", type = Label.class),  
        @XmlElementRef(name = "boxes", type = Boxes.class),  
        @XmlElementRef(name = "optional", type = JAXBElement.class),  
        @XmlElementRef(name = "value", type = JAXBElement.class)  
    })  
    protected List<Object> content;  

    /** 
     * Gets the rest of the content model.  
     *  
     * <p> 
     * You are getting this "catch-all" property because of the following reason:  
     * The field name "Optional" is used by two different parts of a schema. See:  
     * line 102 of file:/C:/WorkSpaces/src/main/resources/webforms.xsd 
     * line 99 of file:/C:/WorkSpaces/src/main/resources/webforms.xsd 
     * <p> 
     * To get rid of this property, apply a property customization to one  
     * of both of the following declarations to change their names:  
     * Gets the value of the content property. 
     *  
     * <p> 
     * This accessor method returns a reference to the live list, 
     * not a snapshot. Therefore any modification you make to the 
     * returned list will be present inside the JAXB object. 
     * This is why there is not a <CODE>set</CODE> method for the content property. 
     *  
     * <p> 
     * For example, to add a new item, do as follows: 
     * <pre> 
     *    getContent().add(newItem); 
     * </pre> 
     *  
     *  
     * <p> 
     * Objects of the following type(s) are allowed in the list 
     * {@link Legend } 
     * {@link JAXBElement }{@code <}{@link String }{@code >} 
     * {@link JAXBElement }{@code <}{@link String }{@code >} 
     * {@link JAXBElement }{@code <}{@link String }{@code >} 
     * {@link JAXBElement }{@code <}{@link String }{@code >} 
     * {@link Content } 
     * {@link Options } 
     * {@link Label } 
     * {@link JAXBElement }{@code <}{@link String }{@code >} 
     * {@link JAXBElement }{@code <}{@link Boolean }{@code >} 
     * {@link Boxes } 
     *  
     *  
     */  
    public List<Object> getContent() {  
        if (content == null) {  
            content = new ArrayList<Object>();  
        }  
        return this.content;  
    }  

}      

在类中,所有作为基本字段的字段都分配了type = JAXBElement.class(ex.-id但它在实际模式中声明为xsd:string) 我试图从对象生成json 由于它被分配给JAXBElement,因此无法正常生成  生成的字段响应
        { “的declaredType”: “java.lang.String中”, “名”: “类型”, “值”: “textarea的”, “范围”: “javax.xml.bind.JAXBElement $ GlobalScope”, “无”:假的,“globalScope”:true,“typeSubstituted”:false}

i am looking to get json response like {type:textarea}

请帮助

1 个答案:

答案 0 :(得分:0)

我通过为下面的字段编写自定义序列化程序来解决这个问题

public class customeSerializer2扩展了JsonSerializer {

@Override
public void serialize(Field field, JsonGenerator jgen,
        SerializerProvider provider) throws IOException,
        JsonProcessingException {
    jgen.writeStartObject();
    JAXBElement jaxbElement;
    for (Object object : field.getContent()) {
        if (object.getClass().isAssignableFrom(JAXBElement.class)) {
            jaxbElement = (JAXBElement) object;
            // jgen.writeFieldName(jaxbElement.getName().toString());
            jgen.writeStringField(jaxbElement.getName().toString(),
                    jaxbElement.getValue().toString());// teRawValue(jaxbElement.getValue().toString());
        } else {
            //jgen.writeStartObject();
            jgen.writeObjectField(object.getClass().getSimpleName(),object);
            //jgen.writeEndObject();
        }
    }
    jgen.writeEndObject();

}
}

并使用像

这样的对象映射器进行注册
SimpleModule module = new SimpleModule("customeSerializerModule",
                new Version(1, 0, 0, null));
        module.addSerializer(Field.class, new customeSerializer2());
        mapper.registerModule(module);