码迷,mamicode.com
首页 > 编程语言 > 详细

spring统一错误响应设置

时间:2019-03-12 18:27:34      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:校验   ESS   val   lse   request   tms   method   handler   对象   

在类入口增加 @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统一错误响应设置

标签:校验   ESS   val   lse   request   tms   method   handler   对象   

原文地址:https://www.cnblogs.com/java-xz/p/10518244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!