【问题标题】:Cannot parse Google geocode json object via webclient无法通过 webclient 解析谷歌地理编码 json 对象
【发布时间】:2021-01-22 14:30:00
【问题描述】:

我尝试调用以下 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*);

来自 google 的 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) 时,我正确地看到了这个对象。

【问题讨论】:

  • 你的错误是?长名,短名
  • 没有解析,对象为空。
  • 您是否尝试过删除所有内容并一次解析一个字段,直到您看到它中断的位置?

标签: java spring spring-webflux spring-webclient


【解决方案1】:

我认为你应该把它解析成一个数组,所以请执行以下操作:

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

【讨论】:

  • 还是有问题。 :-( JSON decoding error: Cannot deserialize instance of [Lcz.reservation.sporty.domain.model.google.PlacesSearchResponse;` 超出 START_OBJECT 令牌;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化 [L.PlacesSearchResponse; 的实例START_OBJECT 令牌`
  • 抱歉,为时已晚 :) 它会告诉哪一行导致错误?
  • at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 1] 我的 get 操作有问题。这是我的第一个 Web 客户端实现。
  • 字段long_nameshort_name 在json 中是snake case,但在java 对象中是camelCase。这两个字段都需要@JsonProperty,这样杰克逊才能正确映射它们。
  • 是的,使用@JsonProperty(value = "long_name") public String longName;@JsonProperty(value = "short_name") public String shortName;
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 2012-05-30
    相关资源
    最近更新 更多