2016-03-19 8 views
0

Bu basit Java Varlığına sahibim ve e-web hizmeti ile ulaştığım JSon çıktısını almam gerekiyor.@JsonRootName çalışmıyor

@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;} 

Ben @JsonRootName eklendi, ama hala bu şekilde benim json çıktısı almak: çıktı bu tür almak çok nihayet, daha benim varlığa eklemek zorunda Ne

[ 
     { 

     }, 

     { 

     } 
    ] 

:

{ 
    "flights": 

    [ 

     { 

     }, 

     { 

     } 
    ] 
    } 

cevap

2

Eğer ObjectMapper

üzerinde apropriate özelliklere ayarlamak zorunda @JsonRootName(value = "flights") kullanmak istiyorsanız Bu

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

üretecek List<Flight> için

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

Ama Yani muhtemelen wraper nesne oluşturmak zorunda:

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

ve bu FlightList{"flights":[{ }, { }]} çıktı json ben eklemek gerekir

+0

olacak Bu kod hizmetimde ise sadece uçuşların listesini döndürüyorum (Liste ) – user5783530

+0

Bence, hangi JavaEE çerçevesini kullandığınıza bağlı. Burada Bahar ise daha fazla bilgi https://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/http/converter/json/JacksonObjectMapperFactoryBean.html veya http: // stackoverflow. com/questions/14343477/nasıl yapılır-global-set-jackson-ignore-bilinmeyen-özellikleri-içinde bahar – varren

+0

@ user5783530 Ben sadece fark, 'JsonRootName' size yardımcı olamaz. Cevabımı güncellendi – varren