响应没有合适的HttpMessageConverter

时间:2015-03-21 05:04:02

标签: android spring resttemplate

我是android的初学者。我需要在我的android项目中调用spring web服务,它返回一个类对象作为response.Below是用于调用Web服务的代码。

int nID=4;
RestTemplate restTemplate=new RestTemplate();
CoaAccountList accList=restTemplate.getForObject("http://localhost:8080/SpringProject/getCoaList/{parentId}", CoaAccountList.class, nID);

以下是网络服务即将到来的控制器代码。

@RequestMapping(value = "/getCoaList/{parentId}",method = RequestMethod.GET)
public @ResponseBody CoaAccountList getCoaList(@PathVariable(value ="parentId") Integer parentId) {
    try{
        System.out.println("coaacclist ");
        CoaAccountList coaAccList=new CoaAccountList();
        List<COAAccount> accountList= saveitDao.getAccounts(parentId);
        coaAccList.setCoaList(accountList);
        return coaAccList;
    }catch(Exception e){
        System.out.println("Exc on list "+e);
        return null;
    }
}

我得到: -

 org.springframework.web.client.ResourceAccessException: Could not extract response: no suitable HttpMessageConverter found for response type[com.example.myandroidproject.CoaAccountList] and content type [application/json].

我不知道如何将此响应转换为android中的CoaAccountList对象。如何做到这一点?请帮助我。我还有另一个返回jsonstring的web服务,它运行正常。

String accList=restTemplate.getForObject("http://localhost:8080/SpringProject/getAllAccounts/{parentId}", String.class, nID);

下面是弹簧配置文件。

servlet的context.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"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:annotation-config />
<mvc:annotation-driven />
<context:component-scan base-package="com.example.save" />

<!-- <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="1" />
</bean> -->

<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver"/>

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/layouts/layouts.xml</value>
            <value>/WEB-INF/layouts/views.xml</value>
        </list>
    </property>
</bean>

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/SpringProject"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
<property name="validationQuery" value="SELECT 1"/>
</bean>

 <!-- Hibernate Session Factory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan">
  <array>
    <value>com.example.save</value>
  </array>
</property>
<property name="hibernateProperties">
  <value>
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
  </value>
</property>
</bean>
<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

1 个答案:

答案 0 :(得分:1)

我可以使用TilesViewResolver;此viewResolver尝试以HTML格式进行响应;我建议您使用org.springframework.web.servlet.view.ContentNegotiatingViewResolver(这里有一篇文章Spring ContentNegotingViewResolver

基本上,关键是spring因为你正在使用TilesViewResolver而尝试用HTML响应;相反,你需要一个JSON响应 在这&#34;混合&#34;一种响应,ContentNegotiatingViewResolver可以帮助你