当前位置: 首页 > 知识库问答 >
问题:

无法从START_OBJECT标记反序列化`java.lang.string`实例(Jackson)

曾珂
2023-03-14

我想从angular 8前端向spring boot API发送一个JSON对象。我是这些框架的新手,我有点迷茫。

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

错误:

“无法将java.lang.String实例从START_OBJECT标记反序列化到[源:(String)”{“coordines”:[{“lat”:76.00542202728906,“lng”:-71.76493508359451},{“lat”:62.96921913888247,“lng”:-113.6539800675124},{“lat”:63.601007712290695,“lng”:-56.583665780107154}]};行:1,列:17](通过引用链to.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.leflet_test.models.dto.querydto”:找不到匹配的编辑器或转换策略

共有1个答案

杭柏
2023-03-14

你只需要做下一个

const it= JSON.stringify(Object);

现在在Spring,webservice应该累加一个字符串,

 类似资料: