对于全局异常需要认识两个注解 @RestControllerAdvice,@ExceptionHandler
1、 @RestControllerAdvice
组成:@ControllerAdvice、@ResponseBody
@RestControllerAdvice特点: (1)通过@ControllerAdvice注解可以将对于控制器的全局配置放在同一个位置,他可以管理所有的Controller。 (2)注解了@RestControllerAdvice的类的方法可以使用@ExceptionHandler、@InitBinder、@ModelAttribute注解到方法上。 (3)@RestControllerAdvice注解将作用在所有注解了@RequestMapping的控制器的方法上。 (4)@ExceptionHandler:用于指定异常处理方法。当与@RestControllerAdvice配合使用时,用于全局处理控制器里的异常。 (5)
- @InitBinder:用来设置WebDataBinder,用于自动绑定前台请求参数到Model中。
- @ModelAttribute:本来作用是绑定键值对到Model中,当与@ControllerAdvice配合使用时,可以让全局的@RequestMapping都能获得在此处设置的键值对**
2、@ExceptionHandler注解我们一般是用来自定义异常的。 可以认为它是一个异常拦截器(处理器)。
@ExceptionHandler(xxx.class)
xxx.class是具体的异常类 例如:NullPointerException 例子
全局异常捕获 自定义异常类 自定义异常类继承RuntimeException
public class Mall4cloudException extends RuntimeException {
private static final long serialVersionUID = 1L;
private Object object;
private ResponseEnum responseEnum;
public Mall4cloudException(String msg) {
super(msg);
}
public Mall4cloudException(String msg, Object object) {
super(msg);
this.object = object;
}
public Mall4cloudException(String msg, Throwable cause) {
super(msg, cause);
}
public Mall4cloudException(ResponseEnum responseEnum) {
super(responseEnum.getMsg());
this.responseEnum = responseEnum;
}
public Mall4cloudException(ResponseEnum responseEnum, Object object) {
super(responseEnum.getMsg());
this.responseEnum = responseEnum;
this.object = object;
}
public Object getObject() {
return object;
}
public ResponseEnum getResponseEnum() {
return responseEnum;
}
}
自定义返回状态码类型
package com.mall4j.cloud.common.response;
public enum ResponseEnum {
OK("00000", "ok"),
SHOW_FAIL("A00001", ""),
METHOD_ARGUMENT_NOT_VALID("A00002", ""),
HTTP_MESSAGE_NOT_READABLE("A00003", "请求参数格式有误"),
UNAUTHORIZED("A00004", "Unauthorized"),
EXCEPTION("A00005", "服务器出了点小差"),
DATA_ERROR("A00007", "数据异常,请刷新后重新操作"),
CLEAN_TOKEN("A00008", "clean token"),
REFRESH_TOKEN_EXIST("A00009", "refresh token exist"),
DATA_INCOMPLETE("A00010", "数据不完整"),
SPU_NOT_EXIST("A01000", "spu not exist"),
SHOP_CART_NOT_EXIST("A02000", "shop cart not exist"),
ORDER_NOT_EXIST("A03000", "order not exist"),
REPEAT_ORDER("A03002", "please don't repeat order"),
ORDER_EXPIRED("A03003", "order expired"),
ORDER_PAYED("A03007", "order payed"),
ORDER_NO_DELIVERY("A03008", "order no delivery"),
NOT_STOCK("A03010", "not stock"),
ORDER_NOT_FINISH_OR_CLOSE("A03011", "order not finish or close"),
ORDER_NOT_PAYED("A03012", "order not payed"),
ORDER_HAS_FAILED("A03013", "order has failed"),
REFUND_NOT_PERMISSION("A03024", "refund not permission"),
REFUND_STATUS_CHECK("A03034", "refund status check"),
SOCIAL_ACCOUNT_NOT_BIND("A04001", "social account not bind"),
BIZ_TEMP_SESSION_KEY_EXPIRE("A04002", "biz temp session key expire"),
ACCOUNT_NOT_REGISTER("A04003", "account not register");
private final String code;
private final String msg;
public String value() {
return code;
}
public String getMsg() {
return msg;
}
ResponseEnum(String code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public String toString() {
return "ResponseEnum{" + "code='" + code + '\'' + ", msg='" + msg + '\'' + "} " + super.toString();
}
}
这个是返回状态码的初始化,就是每个接口都会返回状态码的格式
package com.mall4j.cloud.common.response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.Objects;
public class ServerResponseEntity<T> implements Serializable {
private static final Logger log = LoggerFactory.getLogger(ServerResponseEntity.class);
private String code;
private String msg;
private T data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public boolean isSuccess() {
return Objects.equals(ResponseEnum.OK.value(), this.code);
}
@Override
public String toString() {
return "ServerResponseEntity{" + "code=" + code + ", msg='" + msg + '\'' + ", data=" + data + '}';
}
public static <T> ServerResponseEntity<T> success(T data) {
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
serverResponseEntity.setData(data);
serverResponseEntity.setCode(ResponseEnum.OK.value());
return serverResponseEntity;
}
public static <T> ServerResponseEntity<T> success() {
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
serverResponseEntity.setCode(ResponseEnum.OK.value());
serverResponseEntity.setMsg(ResponseEnum.OK.getMsg());
return serverResponseEntity;
}
public static <T> ServerResponseEntity<T> showFailMsg(String msg) {
log.error(msg);
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
serverResponseEntity.setMsg(msg);
serverResponseEntity.setCode(ResponseEnum.SHOW_FAIL.value());
return serverResponseEntity;
}
public static <T> ServerResponseEntity<T> fail(ResponseEnum responseEnum) {
log.error(responseEnum.toString());
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
serverResponseEntity.setMsg(responseEnum.getMsg());
serverResponseEntity.setCode(responseEnum.value());
return serverResponseEntity;
}
public static <T> ServerResponseEntity<T> fail(ResponseEnum responseEnum, T data) {
log.error(responseEnum.toString());
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
serverResponseEntity.setMsg(responseEnum.getMsg());
serverResponseEntity.setCode(responseEnum.value());
serverResponseEntity.setData(data);
return serverResponseEntity;
}
public static <T> ServerResponseEntity<T> transform(ServerResponseEntity<?> oldServerResponseEntity) {
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
serverResponseEntity.setMsg(oldServerResponseEntity.getMsg());
serverResponseEntity.setCode(oldServerResponseEntity.getCode());
log.error(serverResponseEntity.toString());
return serverResponseEntity;
}
}
接口层会返回固定的格式 后续更新自定义异常处理
|