4

This is my 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

This is my message body writer

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();

The output of JSON format is like this enter image description here

My Questions are:

  1. Why the results seem not correct? I was put @JsonRootName("Facility") and also enable the wrap root feature.
  2. Any part I miss?
Auf
  • 473
  • 2
  • 5
  • 9
  • I think (although I could be mistaken) this is because it's being returned as an element of a LinkedList. If you just returned one FacilityDTO it should work as expected. – Jamie Kelly Jun 10 '14 at 09:37

1 Answers1

16

See Jackson JSON Deserialization with Root Element

According to the above, you need to configure deserialization as follows: mapper.configure(DerializationFeature.UNWRAP_ROOT_VALUE, true);

Community
  • 1
  • 1
PhiLho
  • 40,535
  • 6
  • 96
  • 134