【问题标题】:Which JAX-B annotated class implements following both representations?哪个 JAX-B 注释类实现了两种表示形式?
【发布时间】:2015-09-03 07:27:40
【问题描述】:

哪个 JAX-B 注释类实现了两种表示形式 (与杰克逊的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>

以下类实现 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;
}

以下类实现 XML:

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

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

是否存在两种表示的实现?或者有没有办法将 @XmlElementWrapper 与 JSON 一起使用?

【问题讨论】:

  • 你的问题到底是什么?

标签: java json xml jaxb jackson


【解决方案1】:

我找到了带有JsonProperty注解的解决方案:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多