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 分钟前
8.1 异常概述
java·开发语言
czhc114007566317 分钟前
C# 1124 接收
开发语言·c#
qq_124987075340 分钟前
基于springboot的疾病预防系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
麦烤楽鸡翅41 分钟前
简单迭代法求单根的近似值
java·c++·python·数据分析·c·数值分析
火星数据-Tina1 小时前
低成本搭建体育数据中台:一套 API 如何同时支撑比分网与 App?
java·前端·websocket
lcu1111 小时前
Java 学习38:ArrayList 类
java
q***2511 小时前
Spring Boot 集成 Kettle
java·spring boot·后端
筱顾大牛2 小时前
IDEA使用Gitee来创建远程仓库
java·gitee·intellij-idea
司铭鸿2 小时前
祖先关系的数学重构:从家谱到算法的思维跃迁
开发语言·数据结构·人工智能·算法·重构·c#·哈希算法
懂得节能嘛.2 小时前
【SDK开发实践】从Java编码到阿里云制品仓库部署
java·阿里云·maven