1、SpringMVC处理JSON数据要依赖3个jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9.3</version>
</dependency>
2、SpringMVC配置文件 dispatcher-servlet.xml 中要开启一个注解
<mvc:annotation-driven></mvc:annotation-driven>
3、对应的controller层里面方法要加@ResponseBody注解
@Controller
@RequestMapping("springmvc")
public class hello {
// 测试json
@ResponseBody //把返回的数据变为了json对象 // 告诉springmvc此时的返回值不是一个view页面,而是一个ajax 请求的返回值数据
@RequestMapping(value="testJson")
public List<Student> testJson(){
Student s1 = new Student(1,23,"zs");
Student s2 = new Student(2,24,"ls");
Student s3 = new Student(3,25,"ww");
System.out.println("-----------------------------");
List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);
return students;
}
}