gpt4 book ai didi

java - 在 Spring restTemplate 中超出 START_ARRAY token

转载 作者:行者123 更新时间:2023-12-04 08:28:00 25 4
gpt4 key购买 nike

我有一个 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;
}

}

和这个类:

@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"}}]

最佳答案

您面临的问题与您请求信息的方式有关。

您正在对服务执行 GET 请求,并尝试将响应映射为 GeolocationAddressList 类的实例。

如果服务实际上返回了一个对象,该对象的属性 addresses 对应于 GeolocationAddress 数组。

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

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

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

我认为它应该有效。

关于java - 在 Spring restTemplate 中超出 START_ARRAY token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65166606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com