我正在使用 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 客户端开发的新手,因此将不胜感激。
如果有人知道请。告诉我如何处理响应?