RestTemplate - RestClientException - 无法提取响应 [英] RestTemplate - RestClientException - Could Not Extract Response

查看:1643
本文介绍了RestTemplate - RestClientException - 无法提取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring应用程序中使用RestTemplate与API进行交互。我正在做一个GET请求,期待一些JSON的响应。执行此操作的代码如下所示:

I am using RestTemplate in my Spring application to interact with an API. I am doing a GET request and expecting some JSON in response. The code to do this is as follows:

HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(null, requestHeaders);
ResponseEntity responseLogin = restTemplate.exchange(url, HttpMethod.GET, requestEntity, MyResponse.class);

当我运行它时,它在下面的行中断开:

When I run this its breaks on the following line:

ResponseEntity responseLogin = restTemplate.exchange(url, HttpMethod.GET, requestEntity, MyResponse.class);

我收到以下异常:

I get the following exception:

Could not extract response: no suitable HttpMessageConverter found for response type [com.responses.MyResponse] and content type [application/json;charset=utf-8]

我是RestTemplates的新手,并且通常会使用Java与REST API进行交互,因此任何有关解决此问题的帮助都将得到高度赞赏。 / b>

I'm new to RestTemplates and interacting with REST API with Java in general, so any help in solving this problem will be highly appreciated.

推荐答案

您需要指定您的messageconverter并将其绑定:

You need to specify your messageconverter and bind it:

  <mvc:annotation-driven>
    <mvc:message-converters register-defaults="false">
        <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="jacksonObjectMapper" />
            <property name="supportedMediaTypes">
                <list>
                    <bean class="org.springframework.http.MediaType">
                        <constructor-arg index="0" value="application" />
                        <constructor-arg index="1" value="json" />
                        <constructor-arg index="2" value="UTF-8" />
                    </bean>
                </list>
            </property>

        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

这篇关于RestTemplate - RestClientException - 无法提取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆