@JsonRootName无法正常工作

时间:2014-06-10 04:28:42

标签: java json jaxb jackson root

这是我的FacilityDTO

@JsonRootName("Facility")
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(name = "Facility", propOrder = { "id", "name", "totalQuantity" })
public class FacilityDTO implements Serializable {

private static final long serialVersionUID = 1L;

@XmlElement(required = true)
private String id;
@XmlElement(required = true)
private String name;
@XmlElement(required = true)
private double totalQuantity;

public FacilityDTO() {
}

public FacilityDTO(Facility facility) {
    this.name = facility.getName();
    this.totalQuantity = facility.getTotalQuantity();
    this.id = facility.getId();
}// getters setter

这是我的邮件正文撰稿人

ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk7Module());
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
mapper.writeValue(out, object);
int bytesCount = out.size();
byte[] bytes = out.toByteArray();
entityStream.write(bytes);
entityStream.flush();

JSON格式的输出就像这样 enter image description here

我的问题是:

  1. 为什么结果看起来不正确?我被置于@JsonRootName(" Facility")并且还启用了换行根功能。
  2. 我想念的任何部分?

1 个答案:

答案 0 :(得分:10)

请参阅Jackson JSON Deserialization with Root Element

根据以上所述,您需要按如下方式配置反序列化: mapper.configure(DerializationFeature.UNWRAP_ROOT_VALUE, true);