java异常的捕获总共需要三个类
1.返回类
2.自定义异常类
3.全局异常捕获类
其他自行导入包
public class ResponseResult<T> {
private static final long serialVersionUID = 1L;
private Integer code;
private String msg;
private T data;
/**
* 状态码
*/
public static final String CODE_TAG = "code";
/**
* 返回内容
*/
public static final String MSG_TAG = "msg";
/**
* 数据对象
*/
public static final String DATA_TAG = "data";
/**
* 初始化一个新创建的 ResponseResult 对象,使其表示一个空消息。
*/
public ResponseResult() {
}
public ResponseResult(String msg) {
this.msg = msg;
}
/**
* 初始化一个新创建的 ResponseResult 对象
*
* @param code 状态码
* @param msg 返回内容
*/
public ResponseResult(int code, String msg) {
this.code = code;
this.msg = msg;
}
/**
* 初始化一个新创建的 ResponseResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
*/
public ResponseResult(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static <T> ResponseResult<T> success() {
return ResponseResult.success("操作成功");
}
/**
* 返回成功数据
*
* @return 成功消息
*/
public static <T> ResponseResult<T> success(T data) {
return ResponseResult.success("操作成功", data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @return 成功消息
*/
public static <T> ResponseResult<T> success(String msg) {
return ResponseResult.success(msg, null);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static <T> ResponseResult<T> success(String msg, T data) {
return new ResponseResult<>(HttpStatus.SUCCESS, msg, data);
}
/**
* 返回错误消息
*
* @return 失败消息
*/
public static <T> ResponseResult<T> error() {
return ResponseResult.error("操作失败");
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static <T> ResponseResult<T> error(String msg) {
return ResponseResult.error(msg, null);
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static <T> ResponseResult<T> error(String msg, T data) {
return new ResponseResult<>(HttpStatus.ERROR, msg, data);
}
/**
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 警告消息
*/
public static<T> ResponseResult<T> error(int code, String msg) {
return new ResponseResult<>(code, msg, null);
}
public class BusinessException extends RuntimeException {
private int code;
private String msg;
public BusinessException() {
super();
}
public BusinessException(String msg) {
super(msg);
this.msg = getMsg();
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
@Override
public String toString() {
return msg;
}
@Slf4j
@ResponseBody
@RestControllerAdvice
public class GlobalExceptionHandler {
public final Logger logger = LoggerFactory.getLogger(this.getClass());
public GlobalExceptionHandler() {
}
public void doSomething(Exception e, HttpServletRequest request) {
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({HttpMessageNotReadableException.class})
public ResponseResult handleHttpMessageNotReadableException(Exception e, HttpServletRequest request) {
this.doSomething(e, request);
this.logger.warn("400-参数解析失败,{},{}", e.getMessage(), request.getServletPath());
return ResponseResult.error("参数解析失败");
}
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
public ResponseResult handleHttpRequestMethodNotSupportedException(Exception e, HttpServletRequest request) {
this.doSomething(e, request);
this.logger.warn("405-不支持当前请求方法,{},{}", e.getMessage(), request.getServletPath());
return ResponseResult.error("不支持当前请求方法");
}
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler({HttpMediaTypeNotSupportedException.class})
public ResponseResult handleHttpMediaTypeNotSupportedException(Exception e, HttpServletRequest request) {
this.doSomething(e, request);
this.logger.warn("415-不支持当前媒体类型,{},{}", e.getMessage(), request.getServletPath());
return ResponseResult.error("不支持当前媒体类型");
}
@ExceptionHandler({Exception.class})
public ResponseResult handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {
this.doSomething(e, request);
if (e instanceof BusinessException) {
if (e.getCause() == null) {
this.logger.warn("业务提示,{},{}", e.getMessage(), request.getServletPath());
response.setStatus(200);
} else {
this.logger.error("业务异常," + request.getServletPath(), e);
response.setStatus(500);
}
return ResponseResult.error(e.getMessage());
} else {
this.logger.error("系统异常," + request.getServletPath(), e);
response.setStatus(500);
return ResponseResult.error("系统异常");
}
}
}