【问题标题】:Can you use both @XmlElement and @JsonProperty together inside a POJO class你能在 POJO 类中同时使用 @XmlElement 和 @JsonProperty 吗
【发布时间】:2017-11-29 18:07:11
【问题描述】:

我有一个 json 负载和一个 xml 负载,我想将这两个负载映射到一个 POJO 类中。一个端点返回一个 json,另一个端点返回一个 xml。我可以将两者合并到一个 pojo 类中吗?

{
  "house": 'big',
  "has-large-house": "yes"
}

<completed-houses>
.....
</completed-houses>
public PayloadResponse(
        @JsonProperty("house") final String house,
        @JsonProperty("has-large-house") final String hasLargeHouseList,
        @XmlElement(name="completed-houses") final String completeHouses) {
    this.house = house;
    this.hasLargeHouseList = hasLargeHouseList;
    this.completeHouses = completeHouses;
}

然后是这些属性的 getter 和 setter。

【问题讨论】:

  • That 不是有效的 JSON 文件,也不是有效的 XML 文件,因此没有解析器可以接受,这意味着您关于如何用 Java 做,没有实际意义。
  • 这没有任何意义。它不是有效的 JSON。这不是一个有效的 XML。
  • 您更新的问题也没有任何意义。 JSON 和 XML 甚至不包含相同的值(例如 XML 中没有值),那么您为什么希望它们映射到一个类中呢?

标签: java json xml jackson


【解决方案1】:

是的!您可以在同一个 POJO 中组合 Jackson 和 JAXB 注释,使用 Jackson module for JAXB annotations 以便 Jackson 可以理解 JAXB 注释,并使用 jackson-dataformat-xml 序列化为 XML。

这是一个例子:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.xml.bind.annotation.XmlElement;
import java.io.IOException;
import java.io.StringWriter;

@Data
@NoArgsConstructor
public class PayloadResponse {
    private String house;

    @JsonProperty("has-large-house")
    private boolean largeHouse;

    @XmlElement(name = "completed-houses")
    private String completedHouses;

    public static void main(String[] args) throws IOException {
        ObjectMapper xmlMapper = new XmlMapper();
        JaxbAnnotationModule module = new JaxbAnnotationModule();
        xmlMapper.registerModule(module);

        PayloadResponse response = new PayloadResponse();
        response.setHouse("The White House");
        response.setLargeHouse(true);
        response.setCompletedHouses("1600 Pennsylvania Ave.");

        StringWriter stringWriter = new StringWriter();

        // Serialize value as XML.
        xmlMapper.writeValue(stringWriter, response);
        System.out.println("XML=" + stringWriter);

        // Serialize value as JSON.
        ObjectMapper jsonMapper = new ObjectMapper();
        stringWriter.getBuffer().setLength(0);
        jsonMapper.writeValue(stringWriter, response);
        System.out.println("JSON=" + stringWriter);
    }
}

输出以下内容:

XML=<PayloadResponse>
        <house>The White House</house>
        <has-large-house>true</has-large-house>
        <completed-houses>1600 Pennsylvania Ave.</completed-houses> 
    </PayloadResponse>

JSON={"house":"The White House",
      "completedHouses":"1600 Pennsylvania Ave.",
      "has-large-house":true}

【讨论】:

    猜你喜欢
    • 2023-02-24
    • 1970-01-01
    • 2017-01-29
    • 2021-10-30
    • 2020-12-19
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 2018-02-25
    相关资源
    最近更新 更多