Consuming key value pairs using spring resttemplat

2019-09-18 07:12发布

Ideally I just want a list of strings, or Hashmap String,String :

List<String> = restTemplate.getForObject(url, List.class, urlVariables);

However I receive the error Could not extract response: no suitable HttpMessageConverter found for response type.

I can access the restful api using restclient and retreive the following :

Content-Type    text/javascript; charset=iso-8859-1

the repsonse body is :

[{"name":"lemons"},{"name":"pears"},{"name":"apples"}]

and my restTemplate is defined as follows :

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>                
            </list>
        </property>
    </bean>

2条回答
走好不送
2楼-- · 2019-09-18 07:32

I think you might want to take a look at this:

https://spring.io/guides/gs/consuming-rest/

That page says:

If you see the error Could not extract response: no suitable HttpMessageConverter found for response type [class hello.Quote] it’s possible you are in an environment that cannot connect to the backend service (which sends JSON if you can reach it). Maybe you are behind a corporate proxy? Try setting the standard system properties http.proxyHost and http.proxyPort to values appropriate for your environment.

Which is pretty much what you are getting. I know the original question was back in 2012, but, hopefully, someone else will see this as a possible solution.

查看更多
姐就是有狂的资本
3楼-- · 2019-09-18 07:46

I don't have suitable project for test it, but try:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
  <property name="messageConverters">
    <list>
      <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes" value="text/javascript" />
      </bean>
    </list>
  </property>
</bean>
查看更多
登录 后发表回答