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...");
    }
}
相关推荐
小萌新上大分35 分钟前
SpringCloudGateWay
java·开发语言·后端·springcloud·springgateway·cloudalibaba·gateway网关
PacosonSWJTU2 小时前
python基础-13-处理excel电子表格
开发语言·python·excel
froginwe112 小时前
Perl 条件语句
开发语言
直视太阳2 小时前
springboot+easyexcel实现下载excels模板下拉选择
java·spring boot·后端
Code成立2 小时前
《深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)》第2章 Java内存区域与内存溢出异常
java·jvm·jvm内存模型·jvm内存区域
啥都鼓捣的小yao2 小时前
利用C++编写操作OpenCV常用操作
开发语言·c++·opencv
灼华十一2 小时前
Golang系列 - 内存对齐
开发语言·后端·golang
一 乐2 小时前
实验室预约|实验室预约小程序|基于Java+vue微信小程序的实验室预约管理系统设计与实现(源码+数据库+文档)
java·数据库·微信小程序·小程序·毕业设计·论文·实验室预约小程序
程序媛学姐2 小时前
SpringRabbitMQ消息模型:交换机类型与绑定关系
java·开发语言·spring
努力努力再努力wz2 小时前
【c++深入系列】:类与对象详解(中)
java·c语言·开发语言·c++·redis