JSON 解析错误:无法从 START_OBJECT 令牌反序列化 java.util.ArrayList 的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45663891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:06:18  来源:igfitidea点击:

JSON parse error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

javajsonspringspring-boot

提问by

I'm using spring boot and and spring data in my project and i have two classes:

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

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:

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

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

{"出发":"fff","到达":"ffff","isFreeWayEnabled":false,stations:{"id":1}

采纳答案by Amer Qarabsa

You are trying to deserialize an object into a list. You need Stations to be JSON array

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

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