Consuming key value pairs using spring resttemplate

断了今生、忘了曾经 提交于 2019-12-25 04:06:25

问题


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>

回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/11138346/consuming-key-value-pairs-using-spring-resttemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!