Rest风格快速开发

Rest风格开发简介

简单点来说,Rest风格的开发就是让别人不知道你在做什么,以deleteUserById和selectUserById为例:

普通开发:路径 /users/deleteById?Id=666 /users/selectById?Id=666 别人很容易知道你这是在干什么

Rest风格开发: 无论是查还是删 路径都是 /users/1 要依靠行为动作(get或delete)才能知道我们在干什么

开发流程

重点区分

简化上述流程的新注解

简化后的案例

java 复制代码
package com.example.restproject.controller;

import com.example.restproject.entity.User;
import org.springframework.web.bind.annotation.*;

@RestController//@RestController=@Controller+@ResponseBody
@RequestMapping("users")
public class UserController {
    /*
        单个参数的可以把参数加入路径变量,通过控制访问行为让别人不知道我们在干什么     /user/1
        删   delete
        查   get


        多个参数的一般用json封装,@RequestBody接收参数   路径中什么也不显示   /users
        改   put
        增   post
     */
    @PostMapping
    //多参数的情况,我们选择用json传送
    public void addUser(@RequestBody User user){
        System.out.println("user:"+user);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable int id){
        System.out.println("delete user...");
    }

//    @DeleteMapping("/{id}/{password}")  /users/1/123 别人真不知道我在干啥
//    public void deleteUser(@PathVariable int id,@PathVariable int password){
//        System.out.println("delete user...");
//    }

    @PutMapping
    public void updateUser(@RequestBody User user){
        System.out.println("update user...");
    }

    @GetMapping("/{id}")
    //把id加入路径中
    public void selectUser(@PathVariable int id){
        System.out.println("select user...");
    }
}
相关推荐
极客先躯2 分钟前
高级java每日一道面试题-2025年12月05日-实战篇[Dockerj]-Docker 安装后的默认存储路径是什么?如何修改?
java·docker·默认存储路径在不同系统上的区别·linux overlay2·修改存储路径的理论方法·修改流程中的关键理论点
祁_z5 分钟前
LangSmith 实操指南「Agent 可观测性系统」
java·服务器
终生成长者17 分钟前
04LangChain SQL 问答系统知识点详解
数据库·python·sql·langchain
小雅痞20 分钟前
[Java][Leetcode hard] 76. 最小覆盖子串
java·算法·leetcode
admiraldeworm22 分钟前
c -> true 导致异常返回 404 问题排查
c语言·开发语言
MegaDataFlowers35 分钟前
代码自动生成
java
dllxhcjla42 分钟前
Spring全套
java·后端·spring
qq_375916371 小时前
kettle菜鸟教程
开发语言·kettle
qq_254674411 小时前
Alpine Linux 基于 Debian 等系统的常规 Nginx
开发语言
故事和你911 小时前
洛谷-数据结构2-1-二叉堆与树状数组1
开发语言·数据结构·c++·算法·动态规划·图论