• 【java、springMVC】REST风格


    描述访问网络资源的格式

    传统风格:http://localhost/user/saveUser

    rest风格:http://localhost/user

    优点

    1.隐藏资源访问行为(用行为动作区分操作)

    2.书写简化

    注解解释

    @RequestMapping

    7221e8eaedad4814bb5cb714489081c9.jpg

    @PathVariable 

    7d28e647304543e5813abd6f596e41f6.jpg

    @RestController 

    2bc589c176e54c3f94b4f48c6aba528f.jpg

    简化开发注解 

    94a559cb000e48d8b72838a49983973b.jpg

    入门案例(最基础,有不合理)

    1. @RequestMapping(value = "/users", method = RequestMethod.POST)
    2. @ResponseBody
    3. public String save(@RequestBody User user) {
    4. System.out.println("user save " + user);
    5. return "{'module':'user save'}";
    6. }
    7. @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    8. @ResponseBody
    9. public String delete(@PathVariable Integer id) {
    10. System.out.println("user delete " + id);
    11. return "{'module':'user delete'}";
    12. }
    13. @RequestMapping(value = "/users", method = RequestMethod.PUT)
    14. @ResponseBody
    15. public String update(@RequestBody User user) {
    16. System.out.println("user update " + user);
    17. return "{'module':'user update'}";
    18. }
    19. @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
    20. @ResponseBody
    21. public String getById(@PathVariable Integer id) {
    22. System.out.println("user getById " + id);
    23. return "{'module':'user getById'}";
    24. }
    25. @RequestMapping(value = "/users",method = RequestMethod.GET)
    26. @ResponseBody
    27. public String getAll() {
    28. System.out.println("user getAll ");
    29. return "{'module':'user getAll'}";
    30. }

    快速开发

    1. package org.example.controller;/*
    2. * @Auther:huangzhiyang
    3. * @Date:2023/10/7
    4. * @Description:
    5. */
    6. import org.example.domain.Book;
    7. import org.springframework.web.bind.annotation.*;
    8. @RestController
    9. @RequestMapping("/book")
    10. public class BookController {
    11. @PostMapping
    12. public String save(@RequestBody Book book) {
    13. System.out.println("book save "+book);
    14. return "{'info':'springmvc'}";
    15. }
    16. @DeleteMapping("/{id}")
    17. public String delete( @PathVariable String id) {
    18. System.out.println("book delete"+id);
    19. return "{'info':'springmvc'}";
    20. }
    21. @PutMapping
    22. public String update(@RequestBody Book book) {
    23. System.out.println("book update");
    24. return "{'info':'springmvc'}";
    25. }
    26. }

  • 相关阅读:
    奥运奖牌查询易语言代码
    面试题:Java中为什么只有值传递?
    14.梯度检测、随机初始化、神经网络总结
    Java手写最短路径算法和案例拓展
    docker介绍及入门举例
    Ipad5代可以用电容笔吗?Ipad好用电容笔推荐
    “蔚来杯“2022牛客暑期多校训练营9 A B G (持续更新中)
    【软考】设计模式之适配器模式
    密码学入门——环游密码世界
    Spring MVC 九:Context层级(基于配置)
  • 原文地址:https://blog.csdn.net/David_Hzy/article/details/133688679