I'm using spring boot and and spring data in my project and i have two classes:
@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
}
and the second class:
@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
}
and the controller:
@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 );
}
The problem is when I'm trying to add a new mission with JSON:
{"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,stations:{"id":1}
You are trying to deserialize an object into a list. You need Stations to be JSON array
{"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,stations:[{"id":1}, {"id":2}]}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.