【问题标题】:Cannot deserialize instance of `java.lang.String` out of START_OBJECT token (Jackson)无法从 START_OBJECT 令牌中反序列化 `java.lang.String` 的实例(杰克逊)
【发布时间】:2020-03-20 17:09:29
【问题描述】:

我想将一个 JSON 对象从我的 Angular 8 前端发送到我的 Spring Boot API。 我是这些框架的新手,有点迷茫。

我有一张带有传单的世界地图,我想将多边形的坐标发送到我的后端。

后端将主体作为字符串获取,但我想用这些坐标制作一个对象(第一步,然后是其他东西)。

我搜索了一些解决方案,但没有找到类似的案例。

这里是代码 =>

角度:

getFeed(data): Observable<boolean> {
        var q = {coordinates:data};
        console.log(q);
        return this.http.get<Answer>(`${this.baseUrl}api/searchs?data=`+encodeURIComponent(JSON.stringify(q))).pipe(
            map(res => true),
            catchError(err => {
                console.error(err);
                return of(false);
            })
        );
    }

Spring ModelDTO:(问题肯定存在,不是 ArrayList)

public class QueryDTO {
//  @JsonFormat(shape=JsonFormat.Shape.ARRAY)
    @JsonDeserialize(as=ArrayList.class)
    private ArrayList<String> coordinates=new ArrayList();
    public QueryDTO (ArrayList<String> coo) {
        this.coordinates=coo;
    }
    public QueryDTO() {}
    public ArrayList<String> getCoordinates() {
        return this.coordinates; 
    }

    public void setCoordinate(ArrayList<String> coo) {
        this.coordinates=coo;
    }

}

弹簧控制器:

    @CrossOrigin(origins = "http://localhost:4200")
    @Produces("application/json")
    @RequestMapping(value = "/searchs")

    public Collection<SearchFeedDTO> getFeed(@RequestParam(value = "data") String data) throws JsonMappingException, JsonProcessingException {
        System.out.println(data);
        System.out.println("I'm here");
        final QueryDTO queryDTO = new ObjectMapper().readValue(data, QueryDTO.class);
        System.out.println("you");
        return null;
    }

错误:

“无法在 [Source: (String)"{"coordinates":[{"lat":76.00542202728906,"lng":-71.76493508359451},{"lat": 62.96921913888247,"lng":-113.6539800675124},{"lat":63.601007712290695,"lng":-56.583665780107154}]}"; 行:1,列:17](通过参考链:com.DTOmind.leaflet_test.Model。 .QueryDTO["坐标"]->java.util.ArrayList[0])"

编辑:

更新的控制器:

    @CrossOrigin(origins = "http://localhost:4200")
    @Produces("application/json")
    @RequestMapping(value = "/searchs")

    public Collection<SearchFeedDTO> getFeed(@RequestParam(value = "data") QueryDTO data) throws JsonMappingException, JsonProcessingException {
        System.out.println(data);
        System.out.println("I'm here");
        return null;
    }

坐标类:

@Getter
@Setter
public class CoordinateDTO {
    private int lat;
    private int lng;
    public CoordinateDTO() {}
}


public class QueryDTO {
//  @JsonFormat(shape=JsonFormat.Shape.ARRAY)
    @JsonDeserialize(as=ArrayList.class)
    private ArrayList<CoordinateDTO> coordinates=new ArrayList<CoordinateDTO>();
    public QueryDTO (ArrayList<CoordinateDTO> coo) {
        this.coordinates=coo;
    }
    public QueryDTO() {}
    public ArrayList<CoordinateDTO> getCoordinates() {
        return this.coordinates; 
    }

    public void setCoordinate(ArrayList<CoordinateDTO> coo) {
        this.coordinates=coo;
    }

}

新错误:无法将类型“java.lang.String”的值转换为所需类型“com.freemind.leaflet_test.Models.DTO.QueryDTO”;嵌套异常是 java.lang.IllegalStateException:无法将类型“java.lang.String”的值转换为所需类型“com.freemind.leaflet_test.Models.DTO.QueryDTO”:找不到匹配的编辑器或转换策略

【问题讨论】:

  • 坐标不是字符串列表。它是一个包含具有两个属性的对象的列表
  • 另外你的参数不应该是一个字符串,它应该是 QueryDTO 然后你不要明确地使用 objectMapper。它将由 spring 自己完成
  • @Jens 谢谢,我添加了一个带 lat 和 lng 的类,Getter Setter public class CoordinateDTO { private String lat;私有字符串 lng; public CoordinateDTO() {} } 并通过 QueryDTO 更改字符串,但现在我有另一个问题:无法将类型“java.lang.String”的值转换为所需类型“com.freemind.leaflet_test.Models.DTO.QueryDTO” ;嵌套异常是 java.lang.IllegalStateException:无法将类型“java.lang.String”的值转换为所需类型“com.freemind.leaflet_test.Models.DTO.QueryDTO”:找不到匹配的编辑器或转换策略
  • 为什么不简单地使用 POST 发送一个 Json 对象并让 Jackson 解组它?
  • 您可以将其作为 json 发送。但它应该是 requestBody 的一部分,而不是 url

标签: java spring jackson angular8


【解决方案1】:

你只需要做下一步

const it= JSON.stringify(Object);

现在在春天,网络服务应该收到一个字符串,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-01
    • 2013-10-23
    • 2019-12-13
    • 2019-02-08
    • 2017-09-23
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    相关资源
    最近更新 更多