@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
Posted gemiaomiao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常相关的知识,希望对你有一定的参考价值。
1.自定义异常处理类
**
* 自定义异常处理类
* 针对不同的异常自定义不同的方法
* 环绕通知
* 切面:针对所有的controller中抛出的异常
* 若使用@ControllerAdvice,则不会自动转换为JSON格式
*/
@RestControllerAdvice
public class RestExceptionHandler {
/**
* 业务异常处理
* @param e
* @return ErrorInfo
*/
@ExceptionHandler({BaseBusinessException.class})
public ErrorInfo BusinessExceptionHandler(BaseBusinessException e) {
return new ResponseResultUtil().error(e.getCode(), e.getMessage());
}
2.定义一个用于返回页面结果信息的VO对象类:ErrorInfo
public class ErrorInfo {
private Integer code;
private String message;
public ErrorInfo(){};
public ErrorInfo(Integer code, String message) {
super();
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
3.定义一个对步骤2中 返回信息结果处理的工具类:ResponseResultUtil
/**
* 类名称: ResponseResultUtil <br>
* 类描述:请求响应工具类<br>
* 创建人:GMM <br>
* date 2019/3/11<br>
*/
public class ResponseResultUtil {
/**
* @param code 响应码
* @param message 相应信息
* @description 请求成功返回对象
*/
public final ErrorInfo success(int code, String message) {
return new ErrorInfo(code, message);
}
/**
* @param httpStatus 返回的响应码所对应的枚举类
* @description 请求失败返回对象
*/
public final ErrorInfo error(HttpStatus httpStatus) {
return new ErrorInfo(httpStatus.value(), httpStatus.getReasonPhrase());
}
public final ErrorInfo error(int code, String message) {
return new ErrorInfo(code, message);
}
4.为方便统一管理异常代码和信息之间的关系,建立枚举类:HttpStatus
public enum HttpStatus {
CONTINUE(100, "Continue"),
SWITCHING_PROTOCOLS(101, "Switching Protocols"),
PROCESSING(102, "Processing"),
CHECKPOINT(103, "Checkpoint"),
OK(200, "OK"),
CREATED(201, "Created"),
ACCEPTED(202, "Accepted"),
NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information"),
NO_CONTENT(204, "No Content"),
RESET_CONTENT(205, "Reset Content"),
PARTIAL_CONTENT(206, "Partial Content"),
MULTI_STATUS(207, "Multi-Status"),
ALREADY_REPORTED(208, "Already Reported"),
IM_USED(226, "IM Used"),
MULTIPLE_CHOICES(300, "Multiple Choices"),
MOVED_PERMANENTLY(301, "Moved Permanently"),
FOUND(302, "Found"),
/** @deprecated */
@Deprecated
MOVED_TEMPORARILY(302, "Moved Temporarily"),
SEE_OTHER(303, "See Other"),
NOT_MODIFIED(304, "Not Modified"),
/** @deprecated */
}
5.封装一个基础业务异常类(让所有自定义业务异常类 继承此 基础类):BaseBusinessException
/**
* 类名称: BaseBusinessException <br>
* 类描述: 业务异常父类<br>
* 创建人:GMM <br>
* date 2019/3/11<br>
*/
public class BaseBusinessException extends RuntimeException {
private Integer code;
// 给子类用的方法
public BaseBusinessException(HttpStatus httpStatus) {
this(httpStatus.value(),httpStatus.getReasonPhrase());
}
public BaseBusinessException(Integer code,String message) {
super(message);
this.code = code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
6.service 层抛出
@Service
public class UserLoginServiceImpl implements UserLoginService {
@Autowired
private UserLoginRepsitory userLoginRepsitory;
@Transactional
@Override
public boolean login(String userCode, String password) {
Employee employee=userLoginRepsitory.getEmployeeByCode(userCode);
if(StringUtils.isBlank(userCode)){
throw new BaseBusinessException(101,"用户名不能为空");
}
}
以上是关于@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常的主要内容,如果未能解决你的问题,请参考以下文章