这个快速开发就是在之前入门案例的基础上,做了一个改进,本质的内容是没有变化的,变的就是一些声明。最大的变化就是声明使用哪种访问方式了。这里记录下两个大变化。
①@RestController的使用
@RestController 去代替@ResponseBody、@Controller两个东西。再一个就是之前访问路径不是直接放在每一个方法下面吗,现在也直接提上来
②**Mapping的用法。比如(@PostMapping)
之前要写一大串,现在直接使用什么名字就可以了。那如果后面跟了变量怎么办,直接加在后面的括号里面。比如@DeleteMapping("/{id}")
。
这里其实只是一些变化,以后使用这种快速开发就好,并不难。下面是全部代码留在此处,以防以后需要。
//@ResponseBody
//@Controller
@RestController //一个这个代表了上面两个
@RequestMapping("/user")
public class UserController {
//设置当前请求方法为POST,表示REST风格中的添加操作
// @RequestMapping(method = RequestMethod.POST)
@PostMapping
public String save(@RequestBody User user){
System.out.println("user save...");
return "{'module':'user save'}" + user;
}
//设置当前请求方法为DELETE,表示REST风格中的删除操作
//@PathVariable注解用于设置路径变量(路径参数),要求路径上设置对应的占位符,并且占位符名称与方法形参名称相同
// @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id){
System.out.println("user delete..." + id);
return "{'module':'user delete'}";
}
//设置当前请求方法为PUT,表示REST风格中的修改操作
// @RequestMapping(method = RequestMethod.PUT)
@PutMapping
public String update(@RequestBody User user){
System.out.println("user update..."+user);
return "{'module':'user update'}";
}
//设置当前请求方法为GET,表示REST风格中的查询操作
//@PathVariable注解用于设置路径变量(路径参数),要求路径上设置对应的占位符,并且占位符名称与方法形参名称相同
// @RequestMapping(value = "/{id}" ,method = RequestMethod.GET)
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("user getById..."+id);
return "{'module':'user getById'}";
}
//设置当前请求方法为GET,表示REST风格中的查询操作
// @RequestMapping(method = RequestMethod.GET)
@GetMapping
public String getAll(){
System.out.println("user getAll...");
return "{'module':'user getAll'}";
}
}