RESTFUL风格和传统访问方式区别

很多RESTFUL风格的访问地址都是一样的,只是行为动作区分了,对外隐藏了真实操作

代码示例

复制代码
/**
 * @author hrui
 * @date 2024/8/9 14:26
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
    private Integer id;
    private String name;
}

package com.example.demo1.controller;

import com.example.demo1.pojo.Book;
import org.springframework.web.bind.annotation.*;

/**
 * @author hrui
 * @date 2024/8/8 22:53
 */
@RestController
@RequestMapping("/books")
public class TestController {

    @GetMapping("/{id}")
    public String getBookById(@PathVariable Integer id){
        return "获得Id为 " + id+ " 的图书";
    }


    @GetMapping("/{id}/{name}")
    public String getBookByIdAndName(@PathVariable Integer id,@PathVariable String name){
        return "获得Id为 " + id+ " 的图书,name为"+name;
    }

    @GetMapping("")
    public String getAllBooks(){
        return "获得所有图书";
    }


    @PostMapping("")
    public String saveBook(@RequestBody Book book){
        return "新增图书成功";
    }


    @PutMapping("")
    public String updateBook(@RequestBody Book book){
        return "修改图书成功";
    }


    @DeleteMapping("/{id}")
    public String deleteBook(@PathVariable Integer id){
        return "删除id为 "+id+" 图书成功";
    }

}
相关推荐
用户479492835691538 分钟前
claude Fable用不了?把Gpt 5.5pro接到你的claude code里
前端·后端
GetcharZp3 小时前
告别 Nginx 复杂配置!这款带 Web 面板的万能代理神器,让端口转发变得如此简单
后端
IT_陈寒5 小时前
React的useState居然还有这种坑?我差点删库跑路
前端·人工智能·后端
Pedantic6 小时前
SwiftUI 手势笔记
前端·后端
金銀銅鐵6 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
飘尘9 小时前
前端转型全栈(Java后端)的快速上手指引
前端·后端·全栈
浏览器工程师10 小时前
AI Agent 接浏览器任务,先别让它一路点到底
前端·后端
行者全栈架构师10 小时前
Maven dependency:tree 的 8 个高级用法
java·后端
Chenyiax10 小时前
从一次请求看懂 OkHttp:架构、调度与连接管理
后端