1

I want to send a JSON Object from my angular 8 front-end to my spring boot API. I'm new on those frameworks and I'm a little lost.

I have a world map, with leaflet, and i want to send the coordinates of the polygon to my backend.

The backend get the body as a String, but i want to make an Object with those coordinates(first step, then other stuff).

I search some solution but I don't find similar cases.

Here is the code =>

Angular :

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: (the problem's certainly there, not sur about the 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;
    }

}

Spring Controller:

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

The error:

"Cannot deserialize instance of java.lang.String out of START_OBJECT token↵ at [Source: (String)"{"coordinates":[{"lat":76.00542202728906,"lng":-71.76493508359451},{"lat":62.96921913888247,"lng":-113.6539800675124},{"lat":63.601007712290695,"lng":-56.583665780107154}]}"; line: 1, column: 17] (through reference chain: com.freemind.leaflet_test.Models.DTO.QueryDTO["coordinates"]->java.util.ArrayList[0])"

Edit:

Updated Controller:

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

Coordinates class:

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

}

new error : failed to convert value of type 'java.lang.String' to required type 'com.freemind.leaflet_test.Models.DTO.QueryDTO'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.freemind.leaflet_test.Models.DTO.QueryDTO': no matching editors or conversion strategy found

11
  • Coordinates is not a list of Strings. It is a list which contains objects with two properties Commented Mar 20, 2020 at 17:15
  • Also your paramater sould not be a string it should be QueryDTO Then you do not explizitly use the objectMapper. it will be done by spring itself Commented Mar 20, 2020 at 17:18
  • @Jens Thanks, I add a class with the lat and the lng, Getter Setter public class CoordinateDTO { private String lat; private String lng; public CoordinateDTO() {} } and change the String by the QueryDTO but now i have another issue: failed to convert value of type 'java.lang.String' to required type 'com.freemind.leaflet_test.Models.DTO.QueryDTO'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.freemind.leaflet_test.Models.DTO.QueryDTO': no matching editors or conversion strategy found Commented Mar 20, 2020 at 18:38
  • 1
    Why don't you simply use a POST to send a Json object and let Jackson unmarshal it? Commented Mar 20, 2020 at 19:10
  • 1
    You can send it as json. But than it should be part of the requestBody not as the url Commented Mar 23, 2020 at 6:26

1 Answer 1

-1

you only need to do the next

const it= JSON.stringify(Object);

Now in the spring, the webservice should recieve a string,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.