这是我的 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格式的输出是这样的

我的问题是:
  • 为什么结果似乎不正确?我被放置了@JsonRootName(“Facility”)并启用了换行根功能。
  • 有我想念的部分吗?
  • 最佳答案

    Jackson JSON Deserialization with Root Element

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

    10-08 18:41