java - JSON 解析错误 : Can not deserialize instance of java. util.ArrayList 超出 START_OBJECT token

标签 java json spring spring-boot

我在我的项目中使用 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
}

和 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 );
}

问题是当我尝试使用 JSON 添加新任务时:

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

最佳答案

您正在尝试将对象反序列化到列表中。您需要 Stations 为 JSON 数组

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

关于java - JSON 解析错误 : Can not deserialize instance of java. util.ArrayList 超出 START_OBJECT token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45663891/

相关文章:

java - Spring Rest RequestMethod.GET 返回 400 Bad Request for @RequestParam required=true 丢失时

java - 在java中显示联系人列表程序中的联系人

java - 尝试将 AddAll 添加到 MutableList 时出现不受支持的异常

jquery - 即使 OnResultExecuted 成功调用后,JsonResult 也未返回

jquery - 从JSON获取数据时与其他数据一起获取 "undefined"

java - 将 Applet 集成到 Spring Web 应用程序

java - Arraylist 不断抛出 IndexOutOfBounds

java - 无法从 IntelliJ IDEA 启动 Tomcat

android - 如何解析来自 API 的 JSON 响应

java - 如何使用 Mono & Flux 限制并发 http 请求