【发布时间】: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>
感谢您在这方面的任何帮助。
【问题讨论】:
-
你是如何测试这个资源的?
-
您发送的 http 请求包含不允许
application/json的"Accept: somthing/something标头。然后内容协商失败,因为 spring 不能满足请求。 spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc -
删除
@RequestMapping注解中的produces="application/json",请重试。 -
我试过没有它,但它仍然无法正常工作
标签: java json spring spring-mvc