9

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}

2
  • can you share your the json you want to deserialize? Commented Aug 13, 2017 at 19:11
  • its above: {"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,"stations":{"id":1} Commented Aug 13, 2017 at 19:15

1 Answer 1

16

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}]}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.