0

我有以下元素的架构

    <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 (例如-id,但它在实际模式中被声明为 xsd:string)我试图从没有正确生成的对象生成 json,因为它分配给 JAXBElement 为字段
{"declaredType":"java.lang.String","name":"type","value":"textarea","scope":"javax.xml.bind.JAXBElement$生成的响应GlobalScope","nil":false,"globalScope":true,"typeSubstituted":false}

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

请帮忙

4

1 回答 1

0

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

public class CustomSerializer2 extends JsonSerializer<Field> {

    @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("customSerializerModule",
                new Version(1, 0, 0, null));
        module.addSerializer(Field.class, new CustomSerializer2());
        mapper.registerModule(module);
于 2013-05-13T10:25:14.077 回答