我正在调用REST端点,该端点在响应主体中返回基本JSON,但Content-Type响应头设置为application / javascript。
这是我的标准RestTemplate bean:
RestTemplateBuilder restBuilder = new RestTemplateBuilder();
return restBuilder.setConnectTimeout(connectTimeout)
.setReadTimeout(readTimeout)
.build();
当我使用此模板发出getForObject请求时,默认转换器无法处理响应,我得到异常:
org.springframework.web.client.RestClientException:无法解压缩响应:找不到适合的响应类型[类MyResponseObj]和内容类型[application / javascript]
的HttpMessageConverter是否有一些快速配置我可以添加到我的RestTemplate来处理这个响应并在通常的转换之前?
我目前的工作只是将响应转换为String而不是我的域对象,然后使用ObjectMapper手动创建我的对象,但感觉有点脏。
答案 0 :(得分:0)
您应该能够自定义HttpMessageConverter以使用应用程序/ javascript媒体类型:https://dzone.com/articles/customizing
这样的事可能有用,但我没有测试过:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
jsonConverter.getSupportedMediaTypes().add(new MediaType("application/javascript"));
return jsonConverter;
}
现在我改编的教程主要关注RestController,而不是RestTemplate。如果它不起作用,您可以手动创建转换器对象并在RestTemplate对象上设置HttpMessageConverters,而不是使用bean:
restTemplate.getMessageConverters().add(jsonConverter);
关键是要将正确的转换器映射到正确的媒体类型,在您的案例应用程序/ javascript中。
答案 1 :(得分:0)
我发现的最佳选择如下:
//Create a new converter class, that it's basically the same jackson, but it works with when header is javascript
public class JavaScriptMessageConverter extends AbstractJackson2HttpMessageConverter {
//Add a new mediatype converter
private JavaScriptMessageConverter() {
super(Jackson2ObjectMapperBuilder.json().build(),new MediaType("application","javascript"));
}
}
//Other class...
//Then add the converter to your rest template
restTemplate.getMessageConverters().add(new JavaScriptMessageConverter());
//Of course you can define the class also a bean and have it as singleton
我对照这个完全有您问题的免费API进行了检查: http://api.coindesk.com/v1/bpi/currentprice.json
16-11-2018 12:02:06.885 [http-nio-12000-exec-1] [process,,,] [] INFO i.z.m.w.r.ArchRestLoggingInterceptor.logResponse - BitcoinService, Response with status code: 200, headers: {Content-Type=[application/javascript], Content-Length=[672], Access-Control-Allow-Origin=[*], Cache-Control=[max-age=15], Date=[Fri, 16 Nov 2018 11:02:06 GMT], Expires=[Fri, 16 Nov 2018 11:03:07 UTC], Server=[nginx/1.12.1], X-Powered-By=[Fat-Free Framework], X-Cache=[Miss from cloudfront, MISS from ced01squidp02.replynet.prv], X-Amz-Cf-Id=[v3NAF9Z0C5SiQrgLuAsxDgUQBP1-oLWUviHvS_9mOaFk7XrSAu7ZMQ==], X-Cache-Lookup=[MISS from ced01squidp02.replynet.prv:8080], Via=[1.1 1b96443527f684c809162d975cdd968f.cloudfront.net (CloudFront), 1.1 ced01squidp02.replynet.prv (squid/3.5.21)], Connection=[keep-alive]}, raw response: {"time":{"updated":"Nov 16, 2018 11:02:00 UTC","updatedISO":"2018-11-16T11:02:00+00:00","updateduk":"Nov 16, 2018 at 11:02 GMT"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchan...org","chartName":"Bitcoin","bpi":{"USD":{"code":"USD","symbol":"$","rate":"5,583.3925","description":"United States Dollar","rate_float":5583.3925},"GBP":{"code":"GBP","symbol":"£","rate":"4,363.6892","description":"British Pound Sterling","rate_float":4363.6892},"EUR":{"code":"EUR","symbol":"€","rate":"4,923.4020","description":"Euro","rate_float":4923.402}}}