image.png

@RequestMapping访问路径

  1. @RequestMapping(value = "/users",method = RequestMethod.POST)


@RequestParam用于接收url地址传参或表单传参

@RequestBody用于接收json数据

@PathVariable用于接收路径参数,使用{参数名称}描述路径参数

  1. // 用户单个查询
  2. @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
  3. @ResponseBody
  4. public String getById(@PathVariable Integer id) {
  5. System.out.println("用户查询,id="+id);
  6. return "{'module':'用户单个查询'}";
  7. }
  • 后期开发中,发送请求参数超过1个时,以json格式为主,@RequestBody应用较广
  • 如果发送非json格式数据,选用@RequestParam接收请求参数
  • 采用RESTful进行开发,当参数数量较少时,例如1个,可以采用@PathVariable接收请求路径变量,通常用于传递id值

    @ResponseBody响应体

    1. @ResponseBody
    2. public String save() {
    3. System.out.println("用户保存");
    4. return "{'module':'用户保存'}";
    5. }
    ```java package com.tj.controller;

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*;

@Controller public class UserController { // 用户保存 @RequestMapping(value = “/users”,method = RequestMethod.POST) @ResponseBody public String save() { System.out.println(“用户保存”); return “{‘module’:’用户保存’}”; }

  1. // 用户多个删除
  2. @RequestMapping(value = "/users",method = RequestMethod.DELETE)
  3. @ResponseBody
  4. public String deleteById() {
  5. System.out.println("用户多个删除");
  6. return "{'module':'用户多个删除'}";
  7. }
  8. // 用户单个删除
  9. @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
  10. @ResponseBody
  11. public String deleteById(@PathVariable Integer id) {
  12. System.out.println("用户单个删除,id="+id);
  13. return "{'module':'用户单个删除'}";
  14. }
  15. // 用户单个修改
  16. @RequestMapping(value = "/users/{id}",method = RequestMethod.PUT)
  17. @ResponseBody
  18. public String modify(@PathVariable Integer id) {
  19. System.out.println("用户单个修改,id="+id);
  20. return "{'module':'用户单个修改'}";
  21. }
  22. // 用户单个查询
  23. @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
  24. @ResponseBody
  25. public String getById(@PathVariable Integer id) {
  26. System.out.println("用户查询,id="+id);
  27. return "{'module':'用户单个查询'}";
  28. }
  29. // 用户保存
  30. @RequestMapping(value = "/users",method = RequestMethod.GET)
  31. @ResponseBody
  32. public String getAll() {
  33. System.out.println("用户多个查询");
  34. return "{'module':'用户多个查询'}";
  35. }

}

  1. <a name="RUZNH"></a>
  2. ## REST简化写法
  3. ```java
  4. package com.tj.controller;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.*;
  7. //@Controller
  8. //@ResponseBody
  9. @RestController
  10. @RequestMapping("/books")
  11. public class BookController {
  12. // BOOK保存
  13. // @RequestMapping(method = RequestMethod.POST)
  14. @PostMapping
  15. public String save() {
  16. System.out.println("BOOK保存");
  17. return "{'module':'BOOK保存'}";
  18. }
  19. // BOOK多个删除
  20. // @RequestMapping(method = RequestMethod.DELETE)
  21. @DeleteMapping
  22. public String delete() {
  23. System.out.println("BOOK多个删除");
  24. return "{'module':'BOOK多个删除'}";
  25. }
  26. // BOOK单个删除
  27. // @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  28. @DeleteMapping("/{id}")
  29. public String deleteById(@PathVariable Integer id) {
  30. System.out.println("BOOK单个删除,id=" + id);
  31. return "{'module':'BOOK单个删除'}";
  32. }
  33. // BOOK单个修改
  34. // @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
  35. @PostMapping("/{id}")
  36. public String modify(@PathVariable Integer id) {
  37. System.out.println("BOOK单个修改,id=" + id);
  38. return "{'module':'BOOK单个修改'}";
  39. }
  40. // BOOK单个查询
  41. // @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  42. @GetMapping("/{id}")
  43. public String getById(@PathVariable Integer id) {
  44. System.out.println("BOOK查询,id=" + id);
  45. return "{'module':'BOOK单个查询'}";
  46. }
  47. // BOOK保存
  48. // @RequestMapping(method = RequestMethod.GET)
  49. @GetMapping
  50. public String getAll() {
  51. System.out.println("BOOK多个查询");
  52. return "{'module':'BOOK多个查询'}";
  53. }
  54. }