【发布时间】:2020-12-03 23:10:04
【问题描述】:
我有一个 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;
}
还有这项服务:
public GeolocationAddress searchFromAddress(String address) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
return restTemplate.exchange("http://nominatim.openstreetmap.org/search?" + address, HttpMethod.GET, entity, GeolocationAddress.class).getBody();
}
但我在运行服务时出现此错误:
org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.bonansa.domain.GeolocationAddress] and content type [text/html]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:126)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:998)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:981)
【问题讨论】:
-
即使您设置了
Accept: application/json,服务器还是使用 HTML 响应(请参阅:content type [text/html]),这可能意味着请求失败并且它向您发送了一个显示错误的 HTML 页面,或者可能它需要登录,并且服务器将您发送到登录页面。
标签: java spring spring-boot resttemplate rest