JSON解析错误:无法将java.util.ArrayList的实例反序列化为START_OBJECT标记。

8

我在我的项目中使用了Spring Boot和Spring Data,有两个类:

@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添加新任务时:

{"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,stations:{"id":1}

请注意,原始内容中的JSON格式存在错误,请确保正确使用JSON格式以避免出错。

你能分享一下你想要反序列化的JSON吗? - Amer Qarabsa
它在上面:{"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,"stations":{"id":1}} - user7035864
1个回答

13
您正在尝试将一个对象反序列化为列表。您需要将Stations转换为JSON数组。
{"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,stations:[{"id":1}, {"id":2}]}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接