gpt4 book ai didi

java - RestTemplate - RestClientException - 无法提取响应

转载 作者:行者123 更新时间:2023-12-01 12:38:03 29 4
gpt4 key购买 nike

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

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

当我运行它时,它会在以下行中断:

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

我收到以下异常:

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 进行交互的,因此我们将非常感谢任何解决此问题的帮助。

最佳答案

您需要指定您的消息转换器并绑定(bind)它:

  <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>

关于java - RestTemplate - RestClientException - 无法提取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25387716/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com