处理JSON的步骤
- 加入jar包
- 编写目标方法,使其返回JSON对应的对象或集合
package com.cerr.springmvc.test;
import com.cerr.springmvc.crud.dao.EmployeeDao;
import com.cerr.springmvc.crud.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Collection;
import java.util.List;
@Controller
public class SpringMVCTest1 {
@Autowired
private EmployeeDao employeeDao;
@RequestMapping(value = "/testJson")
public Collection <Employee> testJson(){
return employeeDao.getAll();
}
}
- 在方法上添加
@ResponseBody
注解
添加后的代码如下: