spring统一错误响应设置
Posted java_zhao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring统一错误响应设置相关的知识,希望对你有一定的参考价值。
在类入口增加 @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;
}
}
以上是关于spring统一错误响应设置的主要内容,如果未能解决你的问题,请参考以下文章
[Spring cloud 一步步实现广告系统] 4. 通用代码模块设计
Spring Boot 统一参数校验统一异常统一响应,这才是优雅的处理方式!
Spring Boot 统一参数校验统一异常统一响应,这才是优雅的处理方式!
错误代码:错误域 = NSCocoaErrorDomain 代码 = 3840“JSON 文本没有以数组或对象和允许未设置片段的选项开头。”