【问题标题】:No suitable HttpMessageConverter for Response没有合适的 HttpMessageConverter 用于响应
【发布时间】:2015-05-24 14:59:17
【问题描述】:

我是 android 的初学者。我需要在我的 android 项目中调用一个 Spring Web 服务,它返回一个类对象作为响应。下面是用于调用 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);

下面是spring配置文件

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>

【问题讨论】:

  • 你能把spring配置也发一下吗?您使用的是哪种 spring viewresolver?
  • 我认为问题在于将响应转换为 CoaAccountList

标签: android spring resttemplate


【解决方案1】:

据我所知,您使用的是 TilesViewResolver;此 viewResolver 尝试以 HTML 响应;我建议你使用 org.springframework.web.servlet.view.ContentNegotiatingViewResolver (这里有一篇文章Spring ContentNegotingViewResolver

基本上,由于您使用的是 TilesViewResolver,因此 spring 会尝试以 HTML 响应;相反,您需要 JSON 响应 在这种“混合”类型的响应中,ContentNegotiatingViewResolver 可以帮助您

【讨论】:

  • 所以我需要在我的 spring 配置文件中添加 ContentNegotiatingViewResolver。对吗?那么我需要在调用 web 服务的 android 端添加任何转换器
  • 如何为我的情况配置这个 Spring ContentNegotingViewResolver。你能解释一下
猜你喜欢
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 2012-02-03
  • 2014-09-03
  • 2016-05-15
  • 1970-01-01
  • 2018-08-18
  • 2017-10-31
相关资源
最近更新 更多