我在项目中使用spring boot和spring数据,我有两个类:
@Entity
public class Mission implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
private String departure;
private String arrival;
private Boolean isFreeWayEnabled;
@OneToMany( mappedBy = "mission" )
private List<Station> stations;
// getters and setters
}
和第二节课:
@Entity
public class Station implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
private String station;
@ManyToOne( fetch = FetchType.LAZY )
@JsonBackReference
private Mission mission;
//getters and setters
}
和控制器:
@RequestMapping( value = "mission/addMission", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8" )
public Reponse addMission( @RequestBody Mission mission ) throws ServletException {
if ( messages != null ) {
return new Reponse( -1, messages );
}
boolean flag = false;
try {
application.addMision( mission );
application.addStation( mission.getStations(), mission.getId() );
flag = true;
} catch ( Exception e ) {
return new Reponse( 5, Static.getErreursForException( e ) );
}
return new Reponse( 0, flag );
}
问题是当我尝试使用JSON添加新任务时:
{&#34;离开&#34;:&#34; FFF&#34;&#34;到达&#34;:&#34; FFFF&#34;&#34; isFreeWayEnabled&#34;:假,车站:{&#34; ID&#34;:1}
答案 0 :(得分:2)
您正在尝试将对象反序列化为列表。您需要Station为JSON数组
{"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,stations:[{"id":1}, {"id":2}]}