Java 无法在spring mvc中使用@ResponseBy发送json对象
在尝试使用Spring 4.x获取JSON响应时, 我得到了406错误: “此请求标识的资源只能根据请求的“接受”头()生成具有不可接受特征的响应。” 这是我的环境:Java 无法在spring mvc中使用@ResponseBy发送json对象,java,json,spring,spring-mvc,Java,Json,Spring,Spring Mvc,在尝试使用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 = "/getAll
* 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);
}
<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>
<!-- telling container to take care of annotations stuff -->
<context:annotation-config />
<!-- declaring base package -->
<context:component-scan base-package="com.controller" />
@RequestMapping(value=“/getAllStudents”,method=RequestMethod.GET,products=“application/json”)
@应答器
公共列表getAllExpenses(){
List students=studentDaoImp.getStudents();
留学生;
//返回新的JsonJtableResponse().ok(学生);
}
我的Spring配置文件:
@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);
}
<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>
<!-- telling container to take care of annotations stuff -->
<context:annotation-config />
<!-- declaring base package -->
<context:component-scan base-package="com.controller" />
感谢您在这方面的帮助。将以下bean定义添加到您的spring配置中:
<!-- 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的评论。您似乎还没有初始化列表,请尝试执行以下操作
@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;
@RequestMapping(value=“/getAllStudents”,method=RequestMethod.GET,headers=“Accept=application/json”)
@应答器
公共列表getAllExpenses(){
List students=new ArrayList();
添加(studentDaoImp.getStudents());
留学生;
}按如下方式使用弹簧4:
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;
}
}
您可以选择从Spring配置文件中删除以下代码:
@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);
}
<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>
<!-- telling container to take care of annotations stuff -->
<context:annotation-config />
<!-- declaring base package -->
<context:component-scan base-package="com.controller" />
您是如何测试此资源的?在tomcat上,您发送的http请求包含一个“Accept:somthing/something
头,该头不允许application/json
。然后,由于spring无法满足请求,内容协商失败。删除products=“application/json>“
在@RequestMapping
注释中,请再试一次。我在没有它的情况下尝试过,但仍然不起作用。如果您使用的是maven,我认为您应该重建项目,即mvn clean install
。我没有使用maven。实际上,我没有使用任何东西来处理依赖关系