Программисты JAVA общаются здесь
Anonymous
@JsonRootName не работает так, как я хочу
Сообщение
Anonymous » 14 янв 2025, 13:41
Это мой 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 выглядит следующим образом
[img]https:/ /i.sstatic.net/L2Bi1.png[/img]
Мои вопросы:
Почему результаты кажутся неправильными? Мне добавили @JsonRootName("Facility"), а также включили функцию переноса корня.
Какую часть я пропустил?
Подробнее здесь:
https://stackoverflow.com/questions/241 ... -as-i-want
1736851305
Anonymous
[b]Это мой FacilityDTO[/b] [code]@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 [/code] [b]Это мой автор текста сообщения[/b] [code]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(); [/code] [b]Вывод в формате JSON выглядит следующим образом[/b] [img]https:/ /i.sstatic.net/L2Bi1.png[/img] [b]Мои вопросы:[/b] [list] [*]Почему результаты кажутся неправильными? Мне добавили @JsonRootName("Facility"), а также включили функцию переноса корня. [*]Какую часть я пропустил? [/list] Подробнее здесь: [url]https://stackoverflow.com/questions/24132884/jsonrootname-not-work-as-i-want[/url]