问题:SpringMVC框架下使用ajax与后台进行json数据交互失败
maven中的jar包依赖:
<!--json转换-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
View层code:
<script type="text/javascript">
//请求json 输出json
function requestJson() {
$.ajax({
type:'POST',
url:'${pageContext.request.contextPath}/requestJson.action',
contentType:'application/json;charset=UTF-8',
data:'{"id":" ","name":"手机","price":"999","detail":"","pic":" " ,"createtime":""}',
success:function (msg) {
alert(msg);
}
})
}
//请求key value 输出json
function responseJson() {
$.ajax({
type:'POST',
contentType:'application/json',
url:'${pageContext.request.contextPath}/responseJson.action',
data:'id=&name=手机&price=999&detail=&pic=&createtime=',
success:function (msg) {
alert(msg);
}
})
}
</script>
Controller层code:
public class JsonTest {
@RequestMapping("/requestJson")
public @ResponseBody ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom){
return itemsCustom;
}
@RequestMapping("/responseJson")
public @ResponseBody ItemsCustom responseJson(ItemsCustom itemsCustom){
return itemsCustom;
}
}
解决方案:maven中jar包依赖替换,pom.xml:
<!--json转换-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.4</version>
</dependency>
要开启spring的注解扫描,springmvc.xml中新添加:
<mvc:annotation-driven/>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="plain"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="*"/>
<constructor-arg index="1" value="*"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="*"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application"/>
<constructor-arg index="1" value="json"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
</list>
</property>
</bean>
View层code:
<html>
<head>
<title>json交互测试</title>
<script type="text/javascript" src="../../js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
//请求json 输出json
function requestJson() {
$.ajax({
type:'POST',
url:'${pageContext.request.contextPath}/requestJson.action',
contentType:'application/json;charset=UTF-8',
data:'{"id":" ","name":"手机","price":"999","detail":"","pic":" " ,"createtime":""}',
success:function (msg) {
alert(JSON.stringify(msg));//解决json返回显示object object 将json转换成字符串
}
})
}
//请求key value 输出json
function responseJson() {
$.ajax({
type:'POST',
contentType:'application/json',
url:'${pageContext.request.contextPath}/responseJson.action',
data:'id=&name=手机&price=999&detail=&pic=&createtime=',
success:function (msg) {
alert(JSON.stringify(msg));
}
})
}
</script>
</head>
<body>
<input type="button" οnclick="requestJson()" value="请求json 输出json"/>
<input type="button" οnclick="responseJson()" value="请求key value 输出json"/>
</body>
</html>
原因分析:
1.可能是jar包不全或者冲突
2.springmvc.xml中光使用 <mvc:annotation-driven/> 不能实现功能,需要开启注解功能