Spring MVC 3.2 ajax 406 修正配置

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	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">

	<context:component-scan base-package="*" />
	<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
	<bean id="contentNegotiationManager"
		class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
		<property name="favorPathExtension" value="false" />
		<property name="favorParameter" value="false" />
		<property name="ignoreAcceptHeader" value="false" />
		<property name="mediaTypes">
			<value>
				atom=application/atom+xml
				html=text/html
				json=application/json
				*=*/*
			</value>
		</property>
	</bean>
	...
</beans>

注意其中:mvc-3.2.xsd

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

导入 jackson-core、jackson-mapper两个jar包

Controller中

@RequestMapping(value="/checkUserName")
public @ResponseBody Map<String, Object> checkUserName(@RequestBody User user){
    Map<String, Object> resultMap = new HashMap<String, Object>();
    return resultMap;
}

MVC将自动把前台的JSON数据转换为User,自动把Map转换为JSON返回前台

你可能感兴趣的:(spring,mvc,Ajax,406,3.2)