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...");
    }
}
相关推荐
喜欢打篮球的普通人3 小时前
LLVM后端指令选择
android·java·javascript
某不知名網友3 小时前
C++ 七大排序算法完整讲解
java·算法·排序算法
CodeStats3 小时前
《源纹天书》第一百六十一章至第一百六十五章:开源化宣言、元定义的力量、栈帧重构、容器道场升级、技术债的化身!
java·源纹天书
不良手残3 小时前
MyBatisPlus代码自动生成器
java
SamDeepThinking4 小时前
一次线程池线上故障复盘:四层防线如何避免数据丢失
java·后端·程序员
花生智源4 小时前
Java实现Prompt工程的技巧——从模板到工程化,告别字符串拼接
java·人工智能
北冥you鱼4 小时前
Go语言与默克尔树:区块链数据完整性的基石
开发语言·golang·区块链
折哥的程序人生 · 物流技术专研4 小时前
Java 23 种设计模式:从踩坑到精通 | 番外:策略 vs 模板方法 —— 组合与继承的终极对决
java·策略模式·java面试·comparator·模版方法模式·java设计模式·从踩坑到精通
库克克4 小时前
【C++】类和对象--this指针详解
java·开发语言·c++
SL_staff4 小时前
JVS低代码完整源码解析:如何二次开发自己的表单组件?
java·低代码·vuex