简体   繁体   中英

Which JAX-B annotated class implements following both representations?

Which JAX-B annotated class implements following both representations (with Jackson's JacksonJaxbJsonProvider )?

JSON:

{"propertyList": [ 
     {"key": "aKey", "value": "someValue"}, 
     {"key": "anotherKey", "value": "someOtherValue"}
]}

XML:

 <root>
    <propertyList>
        <property>
            <key>aKey</key>
            <value>someValue</value>
        </property>
        <property>
            <key>anotherKey</key>
            <value>someOtherValue</value>
        </property>
   </propertyList>
</root>

Following classes implement JSON:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlElement
    private List<Property> propertyList;
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Property {

    @XmlElement
    private String key;

    @XmlElement
    private String value;
}

Following class implements XML:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root{

    @XmlElementWrapper(name="propertyList")
    @XmlElement(name="property")
    private List<Property> propertyList;
}

Exists an implementation for both representations? Or is there a way to use the @XmlElementWrapper with JSON?

I found a solution with the JsonProperty annotation:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root{

    @XmlElementWrapper(name="propertyList")
    @XmlElement(name="property")
    @JsonProperty("propertyList")
    private List<Property> propertyList;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM