0

I try to call following webclient query:

    return webClient
            .get()
            .uri(uriBuilder -> uriBuilder
                    .path("/geocode/json")
                    .queryParam("key", google.getApiKey())
                    .queryParam("latlng", String.join(
                            ",",
                            String.valueOf(point.getLat()),
                            String.valueOf(point.getLng()))
                            )
                    .build())
            .accept(MediaType.APPLICATION_JSON)
            .retrieve()
            .onStatus(HttpStatus::isError, RestErrorHandler::manageError)
            .bodyToMono(*PlacesSearchResponse.class*);

REST operation from google returns (https://developers.google.com/maps/documentation/geocoding/overview#GeocodingResponses) following object:

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            }            
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4224764,
               "lng" : -122.0842499
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4238253802915,
                  "lng" : -122.0829009197085
               },
               "southwest" : {
                  "lat" : 37.4211274197085,
                  "lng" : -122.0855988802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "plus_code": {
            "compound_code": "CWC8+W5 Mountain View, California, United States",
            "global_code": "849VCWC8+W5"
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

And problem is that I cannot parse it into my class: PlacesSearchResponse.class

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class PlacesSearchResponse {
    
        public String status;
        public String errorMessage;
        
    
        @JsonProperty(value = "results")
        public List<PlacesSearchResult> results;
    
    
    
    }

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class PlacesSearchResult implements  Serializable{
    
    private static final long serialVersionUID = 1L; 

    @JsonProperty(value = "address_components")
    public AddressComponent addressComponents[];
}

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class AddressComponent implements Serializable {
    
        private static final long serialVersionUID = 1L;
        public String longName;
        public String shortName;
    

        @JsonProperty(value = "types")
        public String[] types;
    
    
    }

I cannot find any mistake and webclient is ok, because when I tried bodyToMono(String.class) then I saw this object correctly.

user1089362
  • 546
  • 7
  • 29

2 Answers2

0

I think you should parse it into an array, so do the following:

...
.bodyToMono(PlacesSearchResponse[].class);
Janos Vinceller
  • 1,208
  • 11
  • 25
  • There is still problem. :-( `JSON decoding error: Cannot deserialize instance of `[Lcz.reservation.sporty.domain.model.google.PlacesSearchResponse;` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `[L.PlacesSearchResponse;` out of START_OBJECT token` – user1089362 Jan 22 '21 at 16:54
  • Sorry, it's too late :) What line would it tell causes the error? – Janos Vinceller Jan 22 '21 at 19:53
  • `at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 1]` There is something bad in my get operation. It's my first webclient implementation. – user1089362 Jan 23 '21 at 13:47
  • 1
    the fields `long_name` and `short_name` are snake case in the json but are camelCase in the java object. You'll need `@JsonProperty` on both of these fields so that Jackson can correctly map them. – Michael McFadyen Jan 23 '21 at 14:03
  • Yeah, use `@JsonProperty(value = "long_name") public String longName;` and `@JsonProperty(value = "short_name") public String shortName;`! – Janos Vinceller Jan 24 '21 at 19:44
0

Finnaly I have following call with objects:

.bodyToMono(PlacesSearchResponse.class);


@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class PlacesSearchResponse {

public String status;
public String errorMessage;

List <PlacesSearchResult> results;

}


@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class PlacesSearchResult implements  Serializable{

@JsonProperty("place_id")
String placeId;

@JsonProperty("address_components")
List<AddressComponent> addressComponents;

@JsonProperty("formatted_address")
String formattedAddress;
user1089362
  • 546
  • 7
  • 29