START_ARRAY 个标记在 Spring 个 restTemplate 中

out of START_ARRAY token in Spring restTemplate

我有一个 SpringBoot 应用程序。使用此配置文件:

@Configuration
public class ApplicationConfig {

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM));
        restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
        return restTemplate;
    }

}

和这个 class:

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(NON_NULL)
public class GeolocationAddress {

    private Integer placeId;
    private String licence;
    private String osmType;
    private Integer osmId;
    private List<String> boundingbox = null;
    private String lat;
    private String lon;
    private String displayName;
    private String _class;
    private String type;
    private Double importance;
    private Address address;
}

还有这个:

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(NON_NULL)
public class GeolocationAddressList {

    private List<GeolocationAddress> addresses;

}

和此服务:

public List<GeolocationAddress> searchFromAddress(String address) {

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>(headers);

    String url = "https://nominatim.openstreetmap.org/?addressdetails=1&q=bakery+in+berlin+wedding&format=json&limit=1";


    GeolocationAddressList geolocationAddressList
            = restTemplate.exchange(url, HttpMethod.GET, entity, GeolocationAddressList.class).getBody();

    return geolocationAddressList.getAddresses();
}

但是当 运行 服务时出现此错误:

org.springframework.web.client.RestClientException: Error while extracting response for type [class com.bonansa.domain.GeolocationAddressList] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.bonansa.domain.GeolocationAddressList` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.bonansa.domain.GeolocationAddressList` out of START_ARRAY token
 at [Source: (PushbackInputStream); line: 1, column: 1]

JSON

[{"place_id":80416066,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"way","osm_id":4308033,"boundingbox":["40.4249225","40.4254227","-3.7133155","-3.7124476"],"lat":"40.42515745","lon":"-3.7128816738834214","display_name":"Plaza de Emilio Jiménez Millas, Argüelles, Moncloa-Aravaca, Madrid, Área metropolitana de Madrid y Corredor del Henares, Comunidad de Madrid, 28001, Comunidad de Madrid","class":"highway","type":"pedestrian","importance":0.42816097156854616,"address":{"road":"Plaza de Emilio Jiménez Millas","quarter":"Argüelles","city_district":"Moncloa-Aravaca","city":"Madrid","municipality":"Área metropolitana de Madrid y Corredor del Henares","state":"Comunidad de Madrid","postcode":"28001","country_code":"es","administrative":"Comunidad de Madrid"}}]

将您的呼叫更改为

GeolocationAddress[] geolocationAddresses
        = restTemplate.exchange(url, HttpMethod.GET, entity, GeolocationAddress[].class).getBody();

这应该有效..

您可以直接将数组传递给:

java.util.Arrays#asList

然后给你 List<GeolocationAddress>

另一个好处是您可以删除 GeolocationAddressList,因为它不再需要了。

您遇到的问题与您请求信息的方式有关。

您正在向服务执行 GET 请求并尝试将响应映射为 GeolocationAddressList class.

的实例

如果服务实际返回一个 属性 addresses 对应于 GeolocationAddress.

数组的对象,您的代码将起作用

但该服务实际上返回了 GeolocationAddress 本身的数组,而 Jackson 对此表示不满 - 因为您表示您正在等待一个对象,但该服务实际上返回了一个数组。

请将您的代码更改为如下所示:

GeolocationAddress[] addresses
        = restTemplate.exchange(url, HttpMethod.GET, entity, GeolocationAddress[].class).getBody();

我觉得应该可以。

使用ParameterizedTypeReference直接映射响应 List<GeolocationAddress> 不需要容器对象。

public List<GeolocationAddress> searchFromAddress(String address) {

  HttpHeaders headers = new HttpHeaders();
  headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
  HttpEntity<String> entity = new HttpEntity<String>(headers);

  String url = "https://nominatim.openstreetmap.org/?addressdetails=1&q=bakery+in+berlin+wedding&format=json&limit=1";

  ParameterizedTypeReference<List<GeolocationAddress>> typeRef = new ParameterizedTypeReference<List<GeolocationAddress>(){};
  List<GeolocationAddress> addresses = restTemplate.exchange(url, HttpMethod.GET, entity, typeRef).getBody();

  return addresses;
}