传统URL风格 和 基于REST风格URL 的异同特点

发布时间 2023-06-27 16:21:27作者: 大树2

传统URL风格 和 基于REST风格URL 的异同特点

1.传统URL风格如下:定义比较复杂,而且将资源的访问行为对外暴露出来了
http://localhost:8080/user/getById?id=1 GET:查询id为1的用户
http://localhost:8080/user/saveUser POST:新增用户
http://localhost:8080/user/updateUser POST:修改用户
http://localhost:8080/user/deleteUser?id=1 GET:删除id为1的用户

2.基于REST风格URL如下:通过URL定位要操作的资源,通过HTTP动词(请求方式)来描述具体的操作
http://localhost:8080/users/1 GET:查询id为1的用户
http://localhost:8080/users POST:新增用户
http://localhost:8080/users PUT:修改用户
http://localhost:8080/users/1 DELETE:删除id为1的用户

3.开发规范-统一响应结果Result
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
private Integer code;//响应码,1 代表成功; 0 代表失败
private String msg; //响应信息 描述字符串
private Object data; //返回的数据
//增删改 成功响应
public static Result success(){
return new Result(1,"success",null);
}
//查询 成功响应
public static Result success(Object data){
return new Result(1,"success",data);
}
//失败响应
public static Result error(String msg){
return new Result(0,msg,null);
}
}