• SpringMVC-Rest风格


    一、简介

    REST(Representational State Transfer),表现形式状态转换,它是一种软件架构风格 当我们想表示一个网络资源的时候,可以使用两种方式:

    传统风格资源描述形式

    • http://localhost/user/getById?id=1 查询id为1的用户信息
    • http://localhost/user/saveUser 保存用户信息

    REST风格描述形式

    • http://localhost/user/1
    • http://localhost/user

    所以REST的优点有:

    • 隐藏资源的访问行为,无法通过地址得知对资源是何种操作
    • 书写简化

    常用的请求:GET , POST , PUT , DELETE。

    • 发送GET请求是用来做查询
    • 发送POST请求是用来做新增
    • 发送PUT请求是用来做修改
    • 发送DELETE请求是用来做删除

    二、具体代码

    原始实现:

    1. //@Controller
    2. //@ResponseBody配置在类上可以简化配置,表示设置当前每个方法的返回值都作为响应体
    3. //@ResponseBody
    4. @RestController //使用@RestController注解替换@Controller与@ResponseBody注解,简化书写
    5. @RequestMapping("/books")
    6. public class BookController {
    7. // @RequestMapping( method = RequestMethod.POST)
    8. @PostMapping //使用@PostMapping简化Post请求方法对应的映射配置
    9. public String save(@RequestBody Book book){
    10. System.out.println("book save..." + book);
    11. return "{'module':'book save'}";
    12. }
    13. // @RequestMapping(value = "/{id}" ,method = RequestMethod.DELETE)
    14. @DeleteMapping("/{id}") //使用@DeleteMapping简化DELETE请求方法对应的映射配置
    15. public String delete(@PathVariable Integer id){
    16. System.out.println("book delete..." + id);
    17. return "{'module':'book delete'}";
    18. }
    19. // @RequestMapping(method = RequestMethod.PUT)
    20. @PutMapping //使用@PutMapping简化Put请求方法对应的映射配置
    21. public String update(@RequestBody Book book){
    22. System.out.println("book update..."+book);
    23. return "{'module':'book update'}";
    24. }
    25. // @RequestMapping(value = "/{id}" ,method = RequestMethod.GET)
    26. @GetMapping("/{id}") //使用@GetMapping简化GET请求方法对应的映射配置
    27. public String getById(@PathVariable Integer id){
    28. System.out.println("book getById..."+id);
    29. return "{'module':'book getById'}";
    30. }
    31. // @RequestMapping(method = RequestMethod.GET)
    32. @GetMapping //使用@GetMapping简化GET请求方法对应的映射配置
    33. public String getAll(){
    34. System.out.println("book getAll...");
    35. return "{'module':'book getAll'}";
    36. }
    37. }

    更新后:

    1. //标准REST风格控制器开发
    2. @RestController
    3. @RequestMapping("/books")
    4. public class BookController2 {
    5. @PostMapping //添加
    6. public String save(@RequestBody Book book){
    7. System.out.println("book save..." + book);
    8. return "{'module':'book save'}";
    9. }
    10. @DeleteMapping("/{id}")
    11. public String delete(@PathVariable Integer id){
    12. System.out.println("book delete..." + id);
    13. return "{'module':'book delete'}";
    14. }
    15. @PutMapping //修改
    16. public String update(@RequestBody Book book){
    17. System.out.println("book update..."+book);
    18. return "{'module':'book update'}";
    19. }
    20. @GetMapping("/{id}") //get是查询
    21. public String getById(@PathVariable Integer id){
    22. System.out.println("book getById..."+id);
    23. return "{'module':'book getById'}";
    24. }
    25. @GetMapping
    26. public String getAll(){
    27. System.out.println("book getAll...");
    28. return "{'module':'book getAll'}";
    29. }
    30. }

    注意:要在SpringConfig配置类中加上 @EnableWebMvc  注解,目前用来解析json格式,此注解功能很多

    1. @Configuration
    2. @ComponentScan("com.itheima.controller")
    3. @EnableWebMvc
    4. public class SpringMvcConfig {
    5. }

  • 相关阅读:
    uni-app scroll-view设置scrollTop为0返回顶部不生效
    Android虚拟机与ClassLoader类加载机制
    2024 年排名前 5 名的 Mac 数据恢复软件分享
    信息学奥赛一本通:1129:统计数字字符个数
    p9 Eureka-搭建eureka服务
    HarmonyOS NEXT应用开发案例——自定义TabBar
    LeetCode简单题之计算应缴税款总额
    扩展ABP的Webhook功能,推送数据到第三方接口(企业微信群、钉钉群等)
    【用户画像】ClickHouse简介、特点、安装和部署
    《C++进阶--11.文件操作》
  • 原文地址:https://blog.csdn.net/m0_61395860/article/details/133388198