标签:ret rest response dem 自定义 str getc 定义 stat
@Getter
public class DemoException extends RuntimeException {
private Integer code;
public DemoException(ResultEnum resultEnum) {
super(resultEnum.getMessage());
this.code = resultEnum.getCode();
}
public DemoException(Integer code, String message) {
super(message);
this.code = code;
}
}
@RestControllerAdvice
public class DemoExceptionHandler {
@ResponseStatus(value = HttpStatus.OK) // 返回给前端的http状态码
@ExceptionHandler(value = DemoException.class)
public ResultVo handlerSellException(DemoException e) {
return ResultVoUtil.error(e.getCode(), e.getMessage());
}
}
@Getter
public enum ResultEnum implements CodeEnum{
PARAM_ERROR(1, "参数不正确"),
TOKEN_ERROR(10, "token无效"),
USER_NOT_EXIST(20, "用户不存在"),
;
private Integer code;
private String message;
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
}
标签:ret rest response dem 自定义 str getc 定义 stat
原文地址:https://www.cnblogs.com/liuweiqc/p/10984359.html