我在德春啓動反序列化類的問題。當我的控制器試圖反序列化它時,它會崩潰。這裏是類:反序列化JSON從休眠
@Entity
@Table(name="trash_cans")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="id")
public class TrashCan {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="TRASH_CAN_ID")
long id;
@Column(name="TRASH_CAN_NAME")
String name;
@ManyToOne
@JoinColumn(name="PLACE_ID")
private Place place;
@OneToMany(mappedBy="trashCan", targetEntity=TrashMeter.class, fetch=FetchType.LAZY)
private List<TrashCan> trashMeterList;
@OneToMany(mappedBy="trashCan", targetEntity=TrashSensor.class, fetch=FetchType.LAZY)
private List<TrashSensor> trashSensorList;
public TrashCan() {
}
public TrashCan(long id, String name) {
super();
this.id = id;
this.name = name;
}
[getters and setters]
}
這取決於這一個:
@Entity
@Table(name="trash_sensor")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="id")
public class TrashSensor {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(name="name")
private String name;
@Column(name="description")
private String description;
@ManyToOne
@JoinColumn(name="TRASH_CAN_ID")
private TrashCan trashCan;
@OneToMany(mappedBy = "trashSensor", targetEntity = Measurement.class, fetch = FetchType.LAZY)
private List<Measurement> measurementList;
public TrashSensor() {
super();
}
和垃圾傳感器取決於這個類:
@Entity
@Table(name="measurement")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Measurement {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(name="value")
private float value;
@Column(name="last_measure")
private LocalDateTime dateTime;
@ManyToOne
@JoinColumn(name="trash_sensor_id")
private TrashCan trashSensor;
public Measurement() {
}
}
我的Controler:
@RequestMapping(value="/trashCan", method=RequestMethod.GET)
public ResponseEntity<Iterable<TrashCan>> getPlaces(){
Iterable<TrashCan> trashCanIterable = trashCansRepository.findAll();
return new ResponseEntity<>(trashCanIterable, HttpStatus.OK);
}
當我打電話給web服務時,我得到這個呃ROR:
Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: could not deserialize (through reference chain: java.util.ArrayList[0]-br.com.simplepass.cleanerway.domain.TrashCan["trashSensorList"]-org.hibernate.collection.internal.PersistentBag[0]-br.com.simplepass.cleanerway.domain.TrashSensor["measurementList"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not deserialize (through reference chain: java.util.ArrayList[0]-br.com.simplepass.cleanerway.domain.TrashCan["trashSensorList"]-org.hibernate.collection.internal.PersistentBag[0]-br.com.simplepass.cleanerway.domain.TrashSensor["measurementList"])
我無法解釋這個錯誤=/
。任何有關這個問題的幫助非常感謝。當您使用實體之間的關係
@OneToMany(mappedBy = "trashSensor", targetEntity = Measurement.class, fetch = FetchType.LAZY)
@JsonIgnore
private List<Measurement> measurementList;
是的...這是不是唯一的place..you必須找出更多的映射,其進入循環,並添加相應@JsonIgnore –
感謝快速回復! 如果我這樣做,我將無法使用measurementList,對不對?我需要這個depency ...所以我不能使用@JsonIgnore –
然後使用@JsonIgnore上measurementList映射 –