5

I have this simple Java Entity and I Need to make it JSon output, which I reach with e web service.

@Entity
@JsonRootName(value = "flights")
public class Flight implements Serializable {

@Transient
private static final long serialVersionUID = 1L;

public Flight() {
    super();
}

public Flight(FlightDestination destinationFrom, FlightDestination destinationTo, Integer flightPrice, Date date,
        Airplane airplaneDetail) {
    super();
    this.destinationFrom = destinationFrom;
    this.destinationTo = destinationTo;
    this.flightPrice = flightPrice;
    this.date = date;
    this.airplaneDetail = airplaneDetail;
}

public Flight(FlightDestination destinationFrom, FlightDestination destinationTo, Integer flightPrice, Date date) {
    super();
    this.destinationFrom = destinationFrom;
    this.destinationTo = destinationTo;
    this.flightPrice = flightPrice;
    this.date = date;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Enumerated(EnumType.STRING)
private FlightDestination destinationFrom;

@Enumerated(EnumType.STRING)
private FlightDestination destinationTo;

private Integer flightPrice;

@Temporal(TemporalType.DATE)
private Date date;

@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
@JoinColumn(name = "airplane_fk")
private Airplane airplaneDetail;}

I added @JsonRootName, but I still get my json output in this way :

    [  
      {   

      },

      { 

      }
   ]

What more I have to add to my entity, so finally to get this kind of output:

    {
     "flights":

     [  

      {   

      },

      { 

      }
    ]
   }
user5783530
  • 221
  • 1
  • 4
  • 16

2 Answers2

8

If you want to use @JsonRootName(value = "flights") you have to set apropriate features on ObjectMapper

ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); 
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);

But, for List<Flight> this will produce

[  
  {"flights": {}},
  {"flights": {}},
  {"flights": {}},
]

So you probably have to create wraper object:

public class FlightList {
    @JsonProperty(value = "flights")
    private ArrayList<Flight> flights;
}

and this FlightList will have {"flights":[{ }, { }]} output json

varren
  • 14,551
  • 2
  • 41
  • 72
  • Where should I add this code if in my service I just return list of flights (List) – user5783530 Mar 20 '16 at 20:45
  • I think it depends on what JavaEE framevork you are using. If it is Spring here is more info https://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/http/converter/json/JacksonObjectMapperFactoryBean.html or http://stackoverflow.com/questions/14343477/how-do-you-globally-set-jackson-to-ignore-unknown-properties-within-spring – varren Mar 21 '16 at 00:04
  • @user5783530 i just realized, that `JsonRootName`probably cant help you. Updated my answer – varren Mar 26 '16 at 12:16
3

you can use following annotations to make a wrapper by using Jackson;

...
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonTypeName("user")
public class LoginResponse {
  private String name;
  private String email;
}

The reponse will be;

{
  "user": {
    "name": "Lucienne",
    "username": "asenocakUser"
  }
}