我正在使用spring mvc和rest开发客户端服务器应用程序。
它的简单计算器服务,其中客户端从服务器调用方法以执行操作。
这是我的其余客户端代码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");
}
}
这是我的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>
我得到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)
------------------------------------------------------------------------
我是Spring Rest客户开发的新手,因此将不胜感激。
如果有人知道。告诉我如何处理回复?
最佳答案
这是因为MappingJacksonHttpMessageConverter未注册到restTemplate。
默认情况下,将注册类路径中存在的所有类型的MessageConverters。
您应该删除xml中的bean restTemplate的属性messageConverters,以具有默认的messageconverters,或者必须将MappingJacksonHttpMessageConverter添加到xml的messageConverters列表中。
希望这可以帮助