Spring MVC 返回json数据报406错误

这段时间研究了下Spring mvc,,在用基于注解的@ResponseBody返回json格式数据的时候,一直报406错误:The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.代码如下:

web.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springMVCDemo04</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 解决中文乱码的过滤器 -->
  <filter>
  	<filter-name>CharacterEncoding</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>CharacterEncoding</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<load-on-startup>1</load-on-startup>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:conf/annotation-mvc.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

控制器Controller:

@Controller
@RequestMapping("/teacher")
public class TeacherController {
	@RequestMapping("/returnTeacherJsonFmt")
	@ResponseBody
	public Teacher returnTeacherJsonFmt(){
		Teacher teacher = new Teacher();
		teacher.setAge(30);
		teacher.setName("张三");
		return teacher;
	}
}


Teacher实体类:

public class Teacher implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -644498344224352944L;
	
	private int age;
	private String name;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

annotation-mvc.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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
 	
	<!-- Spring3.1之前的注解HandlerMapping -->
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
	<!-- spring3.1之前的注解HandlerAdapter -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
			</list>
		</property>
	</bean>
	
 	<bean class="com.xliang.mvc.controller.TeacherController"/>
	
	
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:viewClass="org.springframework.web.servlet.view.JstlView"
		p:prefix="/WEB-INF/jsp/"
		p:suffix=".jsp"/>
		
	
	<!-- 加载转换器 -->
	<mvc:annotation-driven />
	
</beans>

其中,spring jar是3.2.4的, jackson-all 1.7.3

google了下,发现很多人都遇到这个问题,网上也提供了各种方法,说要把@RequestMapping("/teacher")改成@RequestMapping(value="teacher",headers="application/json");还有的说把

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
			</list>
		</property>
</bean>

改成:

	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
					<property name="supportedMediaTypes">
						<value>application/json;charset=UTF-8</value>
					</property>
				</bean>
			</list>
		</property>
	</bean>
结果都一样,406错误,后来看到了某位仁兄的博客上说,把访问的后缀改一下,web.xml配置文件中的<url-pattern>*.html</url-pattern>改成 <url-pattern>*.json</url-pattern>,试了一下,果然成功了,在地址栏输入http://localhost:8080/mvc/teacher/returnTeacherJsonFmt.json,成功返回Teacher的json格式。

但是觉得这种方法不够好,或者说*.json这种后缀令我很爽,于是找找其他解决方法。看了spring 3.2.4官方的reference,还真发现了方法:

原来,

	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
					<property name="supportedMediaTypes">
						<value>application/json;charset=UTF-8</value>
					</property>
				</bean>
			</list>
		</property>
	</bean>
这种配置方式,是基于spring3.1之前的版本的,spring3.1之后的版本推荐的是org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping以及

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping,于是把annotation-mvc.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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
	<!--1、 Spring3.1开始的处理器映射:RequestMappingHandlerMapping -->
 	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
	
	<!-- Spring3.1开始的处理器适配器:RequestMappingHandlerAdapter -->
 	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
 		<property name="messageConverters">
 			<list>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
 			</list>
 		</property>
 	</bean>
 	
	
	<!-- 控制器 -->
 	<bean class="com.xliang.mvc.controller.TeacherController"/>
	
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:viewClass="org.springframework.web.servlet.view.JstlView"
		p:prefix="/WEB-INF/jsp/"
		p:suffix=".jsp"/>
		
	
	<!-- 加载转换器 -->
	<mvc:annotation-driven />
	
</beans>


请求路径还是*.html,在地址栏输入 http://localhost:8080/mvc/teacher/returnTeacherJsonFmt.html,成功返回json格式的数据,jar包什么的也没换,还是原来的。至于网上其他的方法,在我这里试了都没效果。其中,用到MappingJacksonHttpMessageConverter转换器的时候,一定要提供json相关的包,我这里就用了jackson-all-1.7.3.jar包,网上说的其他的包都没用到

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值