发表于: 2019-12-19 23:00:29

2 1373


今天完成的事情:


重写了代码    这次使用reponsebody直接返回json

用了 map方法      来return不同的数据

@Controller
public class CrudController {
Logger logger = Logger.getLogger(CrudController.class);
static Map<String,Object> map = new HashMap<>();
@Autowired
StudentService ss;
// ------------------------------------以下是关于查询的代码---------------------------------------
//通过id查询学生
@ResponseBody
@RequestMapping(value = "/student/{id}", method = RequestMethod.GET)
public Map findId(@PathVariable(value = "id") long id) {
logger.info(id);
try {
Student student = ss.selectStudentId(id);
logger.info(student);
map.put("code", 200);
map.put("message","查询成功");
map.put("student",student);
return map;
} catch (Exception e){
map.put("code",201);
map.put("message","查询失败");
return map;
}
}
//根据姓名查询用户
@ResponseBody
@RequestMapping(value = "/student/name", method = RequestMethod.GET)
public Map findName(@RequestParam String name) {
logger.info(name);
try {
Student student = ss.selectStudentName(name);
logger.info(student);
map.put("code", 200);
map.put("message", "查询成功");
map.put("student", student);
return map;
} catch (Exception e) {
map.put("code", 201);
map.put("message", "查询失败");
return map;
}finally {
System.out.print("程序错误,请检查错误");
}
}
//查询所有用户
@ResponseBody
@RequestMapping(value="/student",method = RequestMethod.GET)
public Map findAll() {
try {
List<Student> list = ss.selectStudent();
logger.info(list);
map.put("code", 200);
map.put("message", "查询成功");
map.put("student", list);
return map;
}catch (Exception e){
map.put("code", 201);
map.put("message", "查询失败");
return map;
}
}
// ------------------------------------以下是关于增加的代码---------------------------------------
@ResponseBody
@RequestMapping(value="/student",method = RequestMethod.POST)
public Map add(Student student) {
logger.info(student);
try {
ss.insertStudent(student);
map.put("code", 200);
map.put("message", "添加成功");
return map;
}catch (Exception e){
map.put("code", 201);
map.put("message", "添加失败");
return map;
}
}
// ------------------------------------以下是关于删除的代码---------------------------------------
@ResponseBody
@RequestMapping(value = "/student/{id}",method = RequestMethod.DELETE)
public Map deleteId(@PathVariable(value = "id") long id) {
logger.info(id);
try {
ss.selectStudent();
map.put("code", 200);
map.put("message", "删除成功");
return map;
}catch (Exception e){
map.put("code", 201);
map.put("message", "删除失败");
return map;
}
}
//--------------------------------------------通过更改一个学员的信息------------------------------------------------------------------
@RequestMapping(value = "/student",method = RequestMethod.PUT)
public Map update(Student student)
{
logger.info(student);
try {
ss.updateStudent(student);
map.put("code", 200);
map.put("message", "更改成功");
return map;
}catch (Exception e){
map.put("code", 201);
map.put("message", "删除失败");
return map;
}
}
}


postman测试


关于返回JSON数据 三 种方式

1.reponsebody    不需要视图解析器  直接返回json数据

@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public Student test() {
Student s = ss.selectStudentId(2);
return s;
}


2. 直接将视图设为json视图

@RequestMapping(value = "/test", method = RequestMethod.GET)
public ModelAndView test() {
ModelAndView mv = new ModelAndView();
Student s = ss.selectStudentId(2);
mv.addObject("s",s);
mv.setView(new MappingJackson2JsonView());
return mv;
}



3. 使用json tag-lib标签  

在返回视图内进行转换

<%@ taglib prefix ="json" uri ="http://www.atg.com/taglibs/json"%>
<html>
<head>
<title>增加用户</title>
</head>
<body>
<json:object>
<json:property name="id" value="${s1.id}"/></br>
<json:property name="name" value="${s1.name}"/></br>
<json:property name="qq" value="${s1.qq}"/></br>
<json:property name="type" value="${s1.type}"/></br>
<json:property name="time" value="${s1.time}"/></br>
<json:property name="stunum" value="${s1.stunum}"/></br>
<json:property name="daily" value="${s1.daily}"/></br>
<json:property name="wish" value="${s1.wish}"/></br>
<json:property name="senior" value="${s1.senior}"/></br>
<json:property name="code" value="${code}"/></br>
<json:property name="message" value="${message}"/></br>
</json:object>







使用: param接受参数



使用:http body接受参数   



可以看到 在上方的POST请求URL中    

param用户信息会显示在上方

body会被隐藏

因此,当用户信息需要保密时应使用body


明天计划的事情:


推进剩余任务



返回列表 返回列表
评论

    分享到