统一返回
1.定义响应枚举类
@AllArgsConstructor
@Getter
public enum ErrorCode {
SUCCESS(200,"请求成功"),
FAIL(400,"请求异常");
private final long code;
private final String message;
}
2.定义响应结果类
@AllArgsConstructor
@Getter
public class Result <T>{
private long code;
private String message;
private T data;
//返回成功
public static <T> Result<T> success(){
return new Result<T>(errorcode.SUCCESS.getCode(),errorcode.SUCCESS.getMessage(),null);
}
public static <T> Result<T> success(T data){
return new Result<T>(errorcode.SUCCESS.getCode(),errorcode.SUCCESS.getMessage(),data);
}
public static <T> Result<T> success(errorcode errorcode , T data){
return new Result<T>(errorcode.SUCCESS.getCode(),errorcode.SUCCESS.getMessage(),data);
}
//返回失败
public static <T> Result<T> fail(){
return new Result<T>(errorcode.FAIL.getCode(),errorcode.FAIL.getMessage(),null);
}
public static <T> Result<T> fail(T data){
return new Result<T>(errorcode.FAIL.getCode(),errorcode.FAIL.getMessage(),data);
}
public static <T> Result<T> fail(errorcode errorcode , T data){
return new Result<T>(errorcode.FAIL.getCode(),errorcode.FAIL.getMessage(),data);
}
}
3.测试
@GetMapping("data")
public Result f(){
return Result.success(100);
}
统一异常处理
1.定义异常枚举类
可以和统一返回枚举类写一起
@AllArgsConstructor
@Getter
public enum ErrorCode {
SUCCESS(200,"请求成功"),
FAIL(400,"请求异常"),
E_100101(100101,"传入参数与接口不匹配"),
E_100102(100102,"验证码错误"),
E_100103(100103,"验证码为空"),
E_100104(100104,"查询结果为空"),
E_100105(100105,"ID格式不正确或超出Long存储范围"),
E_100106(999999,"未知异常");
private final long code;
private final String message;
}
2.定义异常类
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class AppException extends RuntimeException{
private long code;
private String message;
public AppException (ErrorCode errorCode){
this.code=errorCode.getCode();
this.message=errorCode.getMessage();
}
}
3.定义异常响应类
可以使用统一结果返回,也可以自己重写一个
@AllArgsConstructor
@Getter
public class ErrorResponse<T> {
private long code;
private String message;
public static <T> ErrorResponse<T> error(long code ,String message){
return new ErrorResponse<T>(code,message);
}
public static <T> ErrorResponse<T> error(ErrorCode errorcode){
return new ErrorResponse<T>(errorcode.getCode(),errorcode.getMessage());
}
}
4.全局异常处理类
@ControllerAdvice // 对controller进行面向切面编程,可以结合不同的注解实现不同的增强,结合@ExceptionHandler注解处理异常
public class GlobalExceptionHandler {
// 处理异常
@ExceptionHandler(Exception.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse<Object> processException(HttpServletRequest request, HttpServletResponse response, Exception e){
// 判断异常类型
if (e instanceof AppException) {
// 是自定义异常,取错误代码,错误信息
AppException appexception = (AppException) e;
return ErrorResponse.error(appexception.getCode(),appexception.getMessage());
}
return ErrorResponse.error(ErrorCode.E_100106.getCode(),ErrorCode.E_100106.getMessage());
}
}
5.测试
@RestController
public class UserController {
@GetMapping("data")
public Result f(){
int b =10;
if (b==10){
throw new AppException(ErrorCode.E_100103);
}
int i = b / 0;
return Result.success(100);
}
}