1.运行环境
- Spring MVC-4.3
- Spring-4.3
- jdk-1.8
2.jackson相关jar包
- jackson-core-2.98.jar
- jackson-annotations-2.98.jar
- jackson-databind-2.98.jar
3.dispatcher-servlet.xml配置
<!--自动包扫描-->
<context:component-scan base-package="com.cdsxt"/>
<!--mvc注解驱动-->
<mvc:annotation-driven/>
<!--配置默认的静态处理器-->
<mvc:default-servlet-handler/>
4.jsp
<script src="jquery-3.3.1.js"></script>
$.ajax({
url:"${pageContext.request.contextPath}/updateJson",
type:"post",
contentType: "application/json",
dataType: "json",
data:'{"name":"lisi","age":"22","sex":"男"}',
success:function (data) {
alert(data);
}
})
5.编写对应的pojo
成员变量名和json属性名一一对应
@Data
public class User {
private String name;
private int age;
private String sex;
}
6.controller
@RequestMapping("/updateJson")
@ResponseBody
public User updateJson(@RequestBody User user){
System.out.println(user.getName());
return user;
}