栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springBoot-统一异常处理实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springBoot-统一异常处理实现

实现模块:

1.定义响应数据格式

  • 常量
public interface Conf {
	
	Integer SUCCESS = 0;

	
	Integer FAIL = 1;
}
  • 响应体
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R implements Serializable {

	private static final long serialVersionUID = 1L;

	@Getter
	@Setter
//	@ApiModelProperty(value = "返回标记:成功标记=0,失败标记=1")
	private int code;

	@Getter
	@Setter
//	@ApiModelProperty(value = "返回信息")
	private String msg;

	@Getter
	@Setter
//	@ApiModelProperty(value = "数据")
	private T data;

	public static  R ok() {
		return restResult(null, Conf.SUCCESS, null);
	}

	public static  R ok(T data) {
		return restResult(data, Conf.SUCCESS, null);
	}

	public static  R ok(T data, String msg) {
		return restResult(data, Conf.SUCCESS, msg);
	}

	public static  R failed() {
		return restResult(null, Conf.FAIL, null);
	}

	public static  R failed(String msg) {
		return restResult(null, Conf.FAIL, msg);
	}

	public static  R failed(T data) {
		return restResult(data, Conf.FAIL, null);
	}

	public static  R failed(T data, String msg) {
		return restResult(data, Conf.FAIL, msg);
	}

	private static  R restResult(T data, int code, String msg) {
		R apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}

}

2.自定义异常类与全局异常拦截

  • 自定义异常类
public class baseException extends RuntimeException {
	private static final long serialVersionUID = 1L;

	
	private String module;

	
	private Integer code;

	
	private Object[] args;

	
	private String defaultMessage;

	public baseException(String module, Integer code, Object[] args, String defaultMessage) {
		this.module = module;
		this.code = code;
		this.args = args;
		this.defaultMessage = defaultMessage;
	}

	public baseException(String module, Integer code, Object[] args) {
		this(module, code, args, null);
	}

	public baseException(Integer code, String defaultMessage) {
		this(null, code, null, defaultMessage);
	}

	public baseException(Integer code, Object[] args) {
		this(null, code, args, null);
	}

	public baseException(String defaultMessage) {
		this(null, Conf.FAIL, null, defaultMessage); // 默认给 0
	}

	@Override
	public String getMessage() {
		// 以后开启国际化
		
		return defaultMessage;
	}

	public String getModule() {
		return module;
	}

	public Integer getCode() {
		return code;
	}

	public Object[] getArgs() {
		return args;
	}

	public String getDefaultMessage() {
		return defaultMessage;
	}
}
  • 全局异常拦截
package com.guiyun.exception;

@ControllerAdvice
public class DefaultExceptionHandler {

	Logger logger = LoggerFactory.getLogger(DefaultExceptionHandler.class);

	
	@ExceptionHandler(baseException.class)
	@ResponseBody
	public R baseException(baseException e) {
		logger.error(e.getMessage());
		R result = new R<>();
		result.setMsg(e.getDefaultMessage());
		return result;
	}

	
	@ExceptionHandler(ParseException.class)
	public R parseException(ParseException e) {
		logger.error(e.getMessage(), e);
		R result = new R<>();
		result.setMsg(e.getMessage());
		return result;
	}

	
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public R exceptionHandler(Exception e, HttpServletRequest request) {
		logger.error("异常:" + e.getMessage(), e);
		R result = new R<>();
		result.setMsg("系统异常,请稍后再试!");

		return result;
	}

}

原理模块等有时间再补上

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322568.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号