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...");
    }
}
相关推荐
一壶浊酒..1 小时前
Scrape Webpack练习
开发语言·javascript·webpack
ZJH__GO2 小时前
网络编程v4pro--实现聊天室文件传输功能
java·服务器·网络·计算机网络
程序员黑豆2 小时前
Windows 系统 Java 环境变量配置全攻略:解决“不是内部或外部命令”
java·前端·ai编程
命运之光2 小时前
【C语言完整代码】就诊信息管理系统
java·c语言·开发语言
命运之光2 小时前
【C语言完整代码】图书管理系统:图书馆场景下的增删改查实战
c语言·开发语言
猿长大人3 小时前
C# | Serilog 新手入门
开发语言·c#·.net·log
marvelyu3 小时前
每天10分钟学会OceanBase系列(Day 20):跨机房容灾实战——构建多数据中心高可用架构
java·大数据·数据库
在世修行4 小时前
从零打造 C# 工业视觉检测系统(七):串口通信 SerialPort 封装与开发
开发语言·c#·modbus rtu·rs232/rs485
AI人工智能+电脑小能手4 小时前
大白话说Java设计模式-04-工厂方法模式(业务实战篇)
java·设计模式·工厂方法模式·架构设计·代码解耦
BUG指挥官4 小时前
Sa-Token和Spring Security对比
java·后端·spring