【问题标题】:JSON parse error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT tokenJSON 解析错误:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例
【发布时间】:2018-01-21 15:57:32
【问题描述】:

我在我的项目中使用 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 添加新任务时:

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

【问题讨论】:

  • 你能分享你想要反序列化的json吗?
  • 以上:{"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,"stations":{"id":1}

标签: java json spring spring-boot


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-01-17
    • 2020-01-14
    • 2021-03-26
    • 2017-09-22
    • 2020-02-20
    • 2018-02-03
    • 2019-10-16
    相关资源
    最近更新 更多