在类入口增加 @RestControllerAdvice注解。可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute,并应用到所有@RequestMapping、@PostMapping, @GetMapping注解中。
@RestControllerAdvice
@Slf4j
public class BizExceptionHandler { /**
* 参数校验异常处理
*/
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(某某Exception.class)
ResponseVO handleException(某某Exception exception) { return ResponseUtils.fail(exception.getUniteResponseCode(), exception.getMessage());
}
} 响应工具类:
public class ResponseUtils { /**
*
* MethodName : success
* @Description : 构建响应成功对象
* @return
*/
public static <T>ResponseVO<T> success() {
ResponseVO<T> resultVO = new ResponseVO<T>();
resultVO.setCode(ResponseCodeEnum.SUCCESS.getCode());
resultVO.setMessage(ResponseCodeEnum.SUCCESS.getMsg());
resultVO.setStatus(ResponseCodeEnum.RETURN_STATUS_SUCCESS.getCode());
return resultVO;
} /**
*
* MethodName : success
*
* @Description : 构建响应成功VO对象
* @param data
*/
public static <T>ResponseVO<T> success(T data) { ResponseVO<T> resultVO = new ResponseVO<T>();
resultVO.setCode(ResponseCodeEnum.SUCCESS.getCode());
resultVO.setMessage(ResponseCodeEnum.SUCCESS.getMsg());
resultVO.setStatus(ResponseCodeEnum.RETURN_STATUS_SUCCESS.getCode());
resultVO.setData(data);
return resultVO;
} public static <T>ResponseVO<T> success(Integer code, String msg , T data) { ResponseVO<T> resultVO = new ResponseVO<T>();
resultVO.setCode(code+"");
resultVO.setMessage(msg);
resultVO.setStatus(ResponseCodeEnum.RETURN_STATUS_SUCCESS.getCode());
resultVO.setData(data);
return resultVO;
} /**
*
* MethodName : fail
* @Description : 构建响应失败VO对象
*/
public static <T>ResponseVO<T> fail() {
ResponseVO<T> resultVO = new ResponseVO<T>();
resultVO.setCode(ResponseCodeEnum.ERROR.getCode());
resultVO.setMessage(ResponseCodeEnum.ERROR.getMsg());
resultVO.setStatus(ResponseCodeEnum.RETURN_STATUS_FALSE.getCode());
return resultVO;
} /**
*
* MethodName : fail
*
* @Description : 自定义失败VO对象
* @param re
*/
public static <T>ResponseVO<T> fail(ResponseCodeEnum re) { return fail(re, re.getMsg());
} /**
* Refactor of fail method
* @param re
* @param <T>
*/
public static <T>ResponseVO<T> fail(UniteResponseCode re) { return fail(re, re.getMsg());
} /**
* Refactor of fail method
* @param re
* @param message
* @param <T>
*/
public static <T>ResponseVO<T> fail(UniteResponseCode re, String message) {
ResponseVO<T> resultVO = new ResponseVO<T>();
resultVO.setCode(re.getCode());
resultVO.setMessage(message);
resultVO.setStatus(ResponseCodeEnum.RETURN_STATUS_FALSE.getCode()); return resultVO;
} public static <T>ResponseVO<T> fail(Integer code, String message) {
ResponseVO<T> resultVO = new ResponseVO<T>();
resultVO.setCode(String.valueOf( code ));
resultVO.setMessage(message);
resultVO.setStatus(ResponseCodeEnum.RETURN_STATUS_FALSE.getCode()); return resultVO;
}
}
@ApiModel(value = "统一返回对象")
public class ResponseVO<T> implements Serializable { private static final long serialVersionUID = -2977600236425403990L; /**
* 返回结果码,默认为成功
*/
@ApiModelProperty(value = "返回结果码,默认为成功")
private String code = ResponseCodeEnum.SUCCESS.getCode();
/**
* 返回结果描述
*/
@ApiModelProperty(value = "返回结果描述")
private String message = ResponseCodeEnum.SUCCESS.getMsg(); /**
* 返回成功状态 成功T,失败F
*/
@ApiModelProperty(value = "返回成功状态 成功T,失败F")
private String status; /**
* 返回具体数据
*/
@ApiModelProperty(value = "返回具体数据")
private T data; public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public String getStatus() {
return status;
} public void setStatus(String status) {
this.status = status;
} public T getData() {
return data;
} public void setData(T data) {
this.data = data;
} }