restclient缩进html无效,RestClientException:无法提取响应。找不到合适的HttpMessageConverter...

使用curl命令:

curl -u 591bf65f50057469f10b5fd9:0cf17f9b03d056ds0e11e48497e506a2 https://backend.tdk.com/api/devicetypes/59147fd79e93s12e61499ffe/messages

我收到JSON响应:

{"data":[{"device":"18SE62","time":1494516023,"data":"3235","snr":"36.72",...

我将响应保存在一个txt文件中,然后使用杰克逊进行解析,一切都很好

ObjectMapper mapper = new ObjectMapper();

File f = new File(getClass().getResource

("/result.json").getFile());

MessageList messageList = mapper.readValue(f, MessageList.class);

我认为我应该使用RestTemplate得到相同的结果,但事实并非如此

RestTemplate restTemplate = new RestTemplate();

MessageList messageList =

restTemplate.getForObject("http://592693f43c87815f9b8145e9:f099c85d84d4e325a2186c02bd0caeef@backend.tdk.com/api/devicetypes/591570373c87894b4eece34d/messages", MessageList.class);

我出现了错误

Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdk.domain.backend.MessageList] and content type [text/html;charset=iso-8859-1]

at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109)

at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)

at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)

at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)

at com.tdk.controllers.restful.client.RestTemplateExample.main(RestTemplateExample.java:27)

我试图设置contentType:

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity entity = new HttpEntity("parameters", headers);

MessageList messageList =

restTemplate.getForObject(url, entity, MessageList.class);

但是我得到了一个编译错误

The method getForObject(String, Class, Object...) in the type RestTemplate is not applicable for the arguments (String, HttpEntity,

Class)

我还尝试添加一个Jackson消息转换器

List> messageConverters = new ArrayList>();

//Add the Jackson Message converter

messageConverters.add(new MappingJackson2HttpMessageConverter());

//Add the message converters to the restTemplate

restTemplate.setMessageConverters(messageConverters);

MessageList messageList =

restTemplate.getForObject(url, MessageList.class);

但是后来我得到了这个错误:

Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdk.domain.backend.MessageList] and content type [text/html;charset=iso-8859-1]

at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109)

at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)

at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)

at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)

at com.tdk.controllers.restful.client.RestTemplateExample.main(RestTemplateExample.java:51)

我也尝试添加课程

@Configuration

@EnableWebMvc

public class MvcConf extends WebMvcConfigurationSupport {

protected void configureMessageConverters(List> converters) {

converters.add(converter());

addDefaultHttpMessageConverters(converters);

}

@Bean

MappingJackson2HttpMessageConverter converter() {

MappingJackson2HttpMessageConverter converter

= new MappingJackson2HttpMessageConverter();

return converter;

}

}

但我得到了错误:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdk.domain.backend.MessageList] and content type [text/html;charset=iso-8859-1]

at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109)

at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)

at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)

at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值