无法通过webclient解析google geocode json对象

zzzyeukh  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(336)

我尝试调用以下webclient查询:

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操作(https://developers.google.com/maps/documentation/geocoding/overview#geocodingresponses)以下对象:

{
   "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"
}

问题是我无法将它解析到我的类: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;

    }

我找不到任何错误,webclient是正常的,因为当我尝试bodytomono(string.class)时,我正确地看到了这个对象。

w7t8yxp5

w7t8yxp51#

我认为应该将其解析为一个数组,因此请执行以下操作:

...
.bodyToMono(PlacesSearchResponse[].class);
vhmi4jdf

vhmi4jdf2#

最后,我对对象进行了以下调用:

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

相关问题