我在德春启动反序列化类的问题。当我的控制器试图反序列化它时,它会崩溃。这里是类:反序列化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映射 –