在项目下新建common.entity
包,包中包含两个文件Result
数据类,ResultCode
接口文件
Result.class
@Data
@NoArgsConstructor
public class Result {
private boolean success;
private Integer code;
private String message;
private Object data;
private Result(ResultCode code) {
this.success = code.success;
this.code = code.code;
this.message = code.message;
}
public Result(ResultCode code,Object data) {
this.success = code.success;
this.code = code.code;
this.message = code.message;
this.data = data;
}
public Result(Integer code,String message,boolean success) {
this.code = code;
this.message = message;
this.success = success;
}
public static Result SUCCESS(){
return new Result(ResultCode.SUCCESS);
}
public static Result ERROR(){
return new Result(ResultCode.SERVER_ERROR);
}
public static Result FAIL(){
return new Result(ResultCode.FAIL);
}
}
ResultCode
package com.ihrm.common.entity;
public enum ResultCode {
/**
* SUCCESS:成功
* FAIL:失败
*/
SUCCESS(true,10000,"操作成功!"),
//---系统错误返回码-----
FAIL(false,10001,"操作失败"),
UNAUTHENTICATED(false,10002,"您还未登录"),
UNAUTHORISE(false,10003,"权限不足"),
SERVER_ERROR(false,99999,"抱歉,系统繁忙,请稍后重试!");
boolean success;
int code;
String message;
ResultCode(boolean success,int code, String message){
this.success = success;
this.code = code;
this.message = message;
}
public boolean success() {
return this.success;
}
public int code() {
return this.code;
}
public String message() {
return this.message;
}
}
枚举中错误码、包括其他字段的命名,可以根据自己需求更改,这里面只是举例。
另外推荐一个IDEA的插件,
NR Coding Style
,在Marketplace
中搜索p3c
也可以。是阿里巴巴出的一个代码规范化的提示插件。
原文地址:https://www.cnblogs.com/gyyyblog/p/11963821.html
时间: 2024-10-09 06:49:02