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.Stringout 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