无法使用@ResponseBody在spring mvc中发送json对象

时间:2015-11-16 11:35:40

标签: java json spring spring-mvc

尝试使用Spring 4.x在JSON中获取响应时, 我收到406错误:

“此请求标识的资源只能根据请求”accept“标题()生成具有不可接受特征的响应。”

这是我的环境:

* Spring 4.1.6.RELEASE
* included jackson-all-1.9.0.jar
* Tomcat 6.x
* mvc:annotation-driven in Spring configuration XML file

我的控制器:

@RequestMapping(value = "/getAllStudents", method = RequestMethod.GET,produces="application/json")
@ResponseBody
public List<Student> getAllExpenses() {
    List<Student> students= studentDaoImp.getStudents();
    return students;
    //return new JsonJtableResponse().ok(students);
}

我的Spring配置文件:

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


    <!-- telling container to take care of annotations stuff -->
    <context:annotation-config />

    <!-- declaring base package -->
    <context:component-scan base-package="com.controller" />
    <context:component-scan base-package="com" />
    <mvc:annotation-driven />
    <!-- <mvc:resources location="/css/" mapping="/css/**" /> -->
    <mvc:resources mapping="/**" location="/" />


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



    <!-- declare datasource bean -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url"
            value="jdbc:sqlserver://localhost:1433;databaseName=Test;instancename=mssqlserver1;" />
        <property name="username" value="sa" />
        <property name="password" value="abc123" />
    </bean>

    <bean id="StudentDao" class="com.dao.StudentDaoImp">
        <constructor-arg ref="dataSource" />
    </bean>


</beans>

感谢这方面的任何帮助。

3 个答案:

答案 0 :(得分:0)

将以下bean定义添加到spring conf:

<!-- Total customization - see below for explanation. -->
  <bean id="contentNegotiationManager"
             class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
    <property name="favorParameter" value="true" />
    <property name="parameterName" value="mediaType" />
    <property name="ignoreAcceptHeader" value="true"/> <!-- this should solve the problem -->
    <property name="useJaf" value="false"/>
    <property name="defaultContentType" value="application/json" />

    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
       </map>
    </property>
</bean>

请参阅上面zapl评论中的http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

答案 1 :(得分:0)

您似乎没有初始化列表,尝试执行以下操作

@RequestMapping(value = "/getAllStudents", method = RequestMethod.GET,headers = "Accept=application/json")
    @ResponseBody
    public List<Student> getAllExpenses() {
List<Student> students= new ArrayList<Student>();
students.add(studentDaoImp.getStudents());
return students;   

}

答案 2 :(得分:0)

使用Spring 4 RestController如下:

import org.springframework.web.bind.annotation.RestController;

@RestController
public class YourController {

   @RequestMapping(value = "/getAllStudents", method = RequestMethod.GET)
   public List<Student> getAllExpenses() {
    List<Student> students= studentDaoImp.getStudents();
    return students;
 }
}

并在classpath中添加以下依赖项:

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.0</version>
</dependency>
<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.5.0</version>
</dependency>

您可以选择从 Spring配置文件中删除以下代码:

    <!-- telling container to take care of annotations stuff -->
    <context:annotation-config />

    <!-- declaring base package -->
    <context:component-scan base-package="com.controller" />