我有这个简单的Java实体,我需要将它作为JSon输出,我通过e web服务到达。
@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;}
我添加了@JsonRootName
,但我仍然以这种方式得到我的json输出:
[
{
},
{
}
]
我还要添加到我的实体,所以最后得到这种输出:
{
"flights":
[
{
},
{
}
]
}
如果你想使用qazxsw poi,你必须在qazxsw poi上设置适当的功能
@JsonRootName(value = "flights")
但是,对于ObjectMapper
,这将产生
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
所以你可能必须创建包装器对象:
List<Flight>
而这个[
{"flights": {}},
{"flights": {}},
{"flights": {}},
]
将有public class FlightList {
@JsonProperty(value = "flights")
private ArrayList<Flight> flights;
}
输出json