实现效果
//成功
{
"code": 0 //业务状态码
"data":{
"name": "alan"
},
"msg": "ok"
}
//失败
{
"code": 50001 //业务状态码
"data": null,
"msg": "用户返回异常"
}
公共统一处理类
BaseResponse
@Data
public class BaseResponse<T> implements Serializable {
private static final long serialVersionUID = -4765301762535502096L;
private int code;
private T data;
private String message;
private String description;
public BaseResponse(int code, T data, String message, String description) {
this.code = code;
this.data = data;
this.message = message;
this.description = description;
}
public BaseResponse(int code, T data, String message) {
this(code, data, message,"");
}
public BaseResponse(int code, T data) {
this(code, data, "","");
}
public BaseResponse(ErrorCode errorCode){
this(errorCode.getCode(), null, errorCode.getMessage(), errorCode.getDescription());
}
}
ResultUtils
public class ResultUtils {
/**
* 成功
* @param data 返回数据
* @param <T> 泛型
* @return BaseResponse
*/
public static <T> BaseResponse<T> success(T data) {
return new BaseResponse<>(0, data, "ok");
}
/**
* 失败
* @param errorCode 错误码
* @return BaseResponse
*/
public static BaseResponse error(ErrorCode errorCode) {
return new BaseResponse<>(errorCode);
}
/**
* 失败
* @param errorCode 错误码
* @return BaseResponse
*/
public static BaseResponse error(ErrorCode errorCode, String message, String description) {
return new BaseResponse(errorCode.getCode(), null, message, description);
}
/**
* 失败
* @param errorCode 错误码
* @return BaseResponse
*/
public static BaseResponse error(ErrorCode errorCode, String description) {
return new BaseResponse(errorCode.getCode(), errorCode.getMessage(), description);
}
/**
* 失败
* @param code 错误码
* @return BaseResponse
*/
public static BaseResponse error(int code, String message, String description) {
return new BaseResponse(code, null, message, description);
}
}
ErrorCode
public enum ErrorCode {
SUCCESS(0, "ok", ""),
PARAM_ERROR(40000, "请求参数错误", ""),
PARAM_NULL(40001, "请求参数为空", ""),
NOT_LOGIN(40100, "未登录", ""),
NO_AUTH(40101, "权限不足", ""),
SYSTEM_ERROR(50000, "系统内部异常", ""),;
/**
* 错误码
*/
private final int code;
/**
* 错误信息
*/
private final String message;
/**
* 错误描述
*/
private final String description;
ErrorCode(int code, String message, String description) {
this.code = code;
this.message = message;
this.description = description;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
public String getDescription() {
return description;
}
}
BusinessException
public class BusinessException extends RuntimeException{
private static final long serialVersionUID = 92797686650012857L;
private final int code;
private final String description;
public BusinessException(String message, int code, String description){
super(message);
this.code = code;
this.description = description;
}
public BusinessException(ErrorCode errorCode){
super(errorCode.getMessage());
this.code = errorCode.getCode();
this.description = errorCode.getDescription();
}
public BusinessException(ErrorCode errorCode,String description){
super(errorCode.getMessage());
this.code = errorCode.getCode();
this.description = description;
}
public int getCode() {
return code;
}
public String getDescription() {
return description;
}
}
GloableExceptionHandler
@Slf4j
@RestControllerAdvice
public class GloableExceptionHandler {
@ExceptionHandler(BusinessException.class)
public BaseResponse busiessExceptionHandler(BusinessException e){
log.info("业务异常{}",e.getMessage());
return ResultUtils.error(e.getCode(),e.getMessage(), e.getDescription());
}
@ExceptionHandler(RuntimeException.class)
public BaseResponse runtimeExceptinHandler(RuntimeException e){
log.info("运行时异常{}",e.getMessage());
return ResultUtils.error(ErrorCode.SYSTEM_ERROR,e.getMessage(),"");
}
}