1. 通用返回类
import lombok.Data;
import java.io.Serializable;
/** 通用返回类
*/
@Data
public class BaseResponse<T> implements Serializable {
/**
* 返回状态值
*/
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());
}
}
2. 请求返回工具类
package com.yupi.usercenter.common;
/**
* 请求返回工具类
*/
public class ResultUtils {
/**
* 成功
* @param data
* @param <T>
* @return
*/
public static <T> BaseResponse<T> success(T data) {
return new BaseResponse<T>(0,data,"ok");
}
/**
* 失败
* @param errorCode
* @return
*/
public static BaseResponse error(ErrorCode errorCode) {
return new BaseResponse(errorCode);
}
/**
* 失败
* @param errorCode
* @return
*/
public static BaseResponse error(ErrorCode errorCode,String message,String description) {
return new BaseResponse(errorCode.getCode(),null,message,description);
}
/**
* 请求失败
* @param code
* @param message
* @param description
* @return
*/
public static BaseResponse error(int code,String message,String description) {
return new BaseResponse(code,null,message,description);
}
/**
* 失败
* @param errorCode
* @return
*/
public static BaseResponse error(ErrorCode errorCode,String description) {
return new BaseResponse(errorCode.getCode(),null,errorCode.getMessage(),description);
}
}
3. 使用
在controller层直接使用
@GetMapping("/match")
public BaseResponse<List<User>> matchUsers(long num,HttpServletRequest request) {
if (num <= 0 || num > 20) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
User loginUser = userService.getLoginUser(request);
return ResultUtils.success(userService.matchUsers(num,loginUser));
}
4. 统一异常处理
1. 自定义异常枚举类
import lombok.Getter;
import lombok.Setter;
/**
* 异常枚举
*
*/
@Getter
public enum ErrorCode {
SUCCESS(0, "ok", ""),
PARAMS_ERROR(40000, "请求参数错误", ""),
NULL_ERROR(40001, "请求数据为空", ""),
NOT_LOGIN(40100, "未登录", ""),
NO_AUTH(40101, "无权限", ""),
FORBIDDEN(40300, "禁止访问", ""),
SYSTEM_ERROR(50000, "系统内部错误", ""),
OPERATION_FAILED(50100, "操作失败", "");
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;
}
}
2. 封装业务异常类(自定义异常)
/**
* 业务异常封装类
*/
@Data
public class BusinessException extends RuntimeException{
private int code;
private 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;
}
}
3. 全局异常处理
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 全局异常处理类
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(BusinessException.class)
public BaseResponse businessExceptionHandler(BusinessException e) {
log.error("businessException:"+e.getMessage(),e);
return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());
}
@ExceptionHandler(RuntimeException.class)
public BaseResponse runtimeExceptionHandler(RuntimeException e) {
log.error("runtimeException",e);
return ResultUtils.error(ErrorCode.SYSTEM_ERROR,e.getMessage(),"");
}
}