【问题标题】:unable to use @ResponseBody to send json object in spring mvc无法使用@ResponseBody 在 spring mvc 中发送 json 对象
【发布时间】:2015-11-16 11:35:40
【问题描述】:

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

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

这是我的环境:

* 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>

感谢您在这方面的任何帮助。

【问题讨论】:

标签: java json spring spring-mvc


【解决方案1】:

将以下 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

【讨论】:

  • 我认为你应该重建你的项目,即mvn clean install如果你使用的是maven。
  • 我没有使用 Maven。我实际上并没有使用任何东西来处理依赖关系
【解决方案2】:

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

@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;   

}

【讨论】:

    【解决方案3】:

    使用 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" />
    

    【讨论】:

      猜你喜欢
      • 2015-11-11
      • 1970-01-01
      • 2016-07-08
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 2011-05-21
      • 2015-04-24
      • 2017-11-07
      相关资源
      最近更新 更多