gpt4 book ai didi

java - @JsonRootName 无法正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:39:00 26 4
gpt4 key购买 nike

这是我的 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") 并且还启用了 wrap root 功能。
  2. 我漏掉了什么部分?

最佳答案

参见 Jackson JSON Deserialization with Root Element

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

关于java - @JsonRootName 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24132884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com