java Spring Rest Client 实现:无法提取响应:没有找到适合 xstreamMarshaller 的响应类型的 HttpMessageConverter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13819630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Rest Client implementation : Could not extract response: no suitable HttpMessageConverter found for response type with xstreamMarshaller
提问by Bhushan Baviskar
I am developing client server application using spring mvc and rest. Its simple calculator service in which client calls the methods from server to execute operations.
我正在使用 spring mvc 和 rest 开发客户端服务器应用程序。其简单的计算器服务,其中客户端从服务器调用方法来执行操作。
This is my rest client code restClient.java:
这是我的休息客户端代码 restClient.java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.blog.samples.client;
/**
*
* @author bhushan.baviskar
*/
import com.blog.samples.domain.Calculator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.client.RestTemplate;
public class restClient {
public static void main(String [] args)
{
restClient tmp = new restClient();
tmp.calltoserver();
}
public void calltoserver() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("../../../../appContext.xml", restClient.class);
RestTemplate restTemplate = applicationContext.getBean("restTemplate", RestTemplate.class);
String url = "http://localhost:8080/rest/calc/4&3&+";
Calculator calObj = (Calculator) restTemplate.getForObject(url, Calculator.class);
System.out.println("details " + calObj.getDetails());
System.out.println("done");
}
}
And this is my appContext.xml file :
这是我的 appContext.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<bean id="messageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="xstreamMarshaller" />
<property name="unmarshaller" ref="xstreamMarshaller" />
</bean>
</property>
</bean>
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<props>
<prop key="Calculator">com.blog.samples.webservices.rest.CalcController</prop>
</props>
</property>
</bean>
</beans>
I am getting response in json format but when I execute the restclient.java file it says :
我收到 json 格式的响应,但是当我执行 restclient.java 文件时,它说:
DEBUG: [Dec-11 16:54:39,706] web.client.RestTemplate - GET request for "http://localhost:8080/rest/calc/4&3&+" resulted in 200 (OK)
Exception in thread "main" org.springframework.web.client.RestClientException: **Could not extract response: no suitable HttpMessageConverter found for response type** [com.blog.samples.domain.Calculator] and content type [text/plain;charset=UTF-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:84)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:446)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
at com.blog.samples.client.restClient.calltoserver(restClient.java:27)
at com.blog.samples.client.restClient.main(restClient.java:21)
------------------------------------------------------------------------
I am new to the Spring Rest client Development so any help will be appreciated.
我是 Spring Rest 客户端开发的新手,因此将不胜感激。
If anyone knows pl. tell me How to handle the response?
如果有人知道pl。告诉我如何处理响应?
回答by akshay
This is because the MappingHymansonHttpMessageConverter is not registered to your restTemplate. By default all types of MessageConverters present in your classpath will be registered.
这是因为 MappingHymansonHttpMessageConverter 未注册到您的 restTemplate。默认情况下,您的类路径中存在的所有类型的 MessageConverters 都将被注册。
You should either remove property messageConverters for bean restTemplate in you xml to have default messageconverters or you have to add MappingHymansonHttpMessageConverter to your messageConverters list in your xml.
您应该在 xml 中删除 bean restTemplate 的属性 messageConverters 以获得默认消息转换器,或者您必须将 MappingHymansonHttpMessageConverter 添加到您的 xml 中的 messageConverters 列表中。
Hope this helps
希望这可以帮助