1.自定义异常类
package com.dwl.exception;
import com.dwl.enums.RespCodeEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class BusinessException extends RuntimeException{
private String code;
private String message;
public BusinessException(){
super();
}
public BusinessException(String code ,String message){
super(message);
this.code = code;
this.message = message;
}
public BusinessException(RespCodeEnum error){
super(error.getDesc());
this.code = error.getCode();
this.message = error.getDesc();
}
}
2.自定义异常枚举
package com.dwl.enums;
import lombok.Getter;
@Getter
public enum RespCodeEnum {
SUCCESS("0000","请求成功"),
SYSTEM_ERROR("0001","系统异常"),
NAME_OR_PASSWORD_ERROR("1000","用户名或者密码错误"),
PARAM_ERROR("1001","参数错误"),
EMPTY_ERROR("1002","结果为空"),
TOKEN_EXP("1003","token已过期,请重新登录"),
TOKEN_ERROR("1004","token错误"),
ACCESS_DENIED("1005","无权限访问!"),
;
private String code;
private String desc;
RespCodeEnum(String code,String desc){
this.code = code;
this.desc = desc;
}
}
3.使用spring的@RestControllerAdvice统一处理异常
package com.dwl.config;
import com.dwl.enums.RespCodeEnum;
import com.dwl.exception.BusinessException;
import com.dwl.pojo.dto.ResponseDto;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@RestControllerAdvice
@Slf4j
public class GlobalExceptionConfig {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseDto methodArgumentNotValid(MethodArgumentNotValidException ex){
log.error("参数异常:",ex);
List<ObjectError> allErrors = ex.getBindingResult().getAllErrors();
List<String> errorMessage = new ArrayList<>();
Optional.of(allErrors).ifPresent(errors->errors.forEach(v->errorMessage.add(v.getDefaultMessage())));
return ResponseDto.paramError(Joiner.on(",").join(errorMessage));
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseDto httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex){
log.error("请求方式异常:",ex);
String message = ex.getMessage();
return ResponseDto.paramError(message);
}
@ExceptionHandler(BusinessException.class)
public ResponseDto exception(BusinessException ex){
log.error("业务异常",ex);
return ResponseDto.fail(ex.getCode(),ex.getMessage());
}
@ExceptionHandler(Exception.class)
public ResponseDto exception(Exception ex){
log.error("系统异常",ex);
return ResponseDto.fail(RespCodeEnum.SYSTEM_ERROR);
}
@ExceptionHandler(AccessDeniedException.class)
public ResponseDto accessDeniedException(AccessDeniedException ex){
log.error("权限异常:",ex);
return ResponseDto.fail(RespCodeEnum.ACCESS_DENIED);
}
}