我在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进行交互,因此我们将非常感谢您解决此问题的任何帮助。
答案 0 :(得分:1)
您需要指定messageconverter并将其绑定:
<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>