基于spring boot的统一异常处理
Posted knyel
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于spring boot的统一异常处理相关的知识,希望对你有一定的参考价值。
一、springboot的默认异常处理
Spring Boot提供了一个默认的映射:/error
,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异常内容。
例如这里我们认为制造一个异常
@GetMapping(value = "/boys") public List<Boy> boyList() throws Exception{ throw new Exception("错误"); }
使用浏览器访问http://127.0.0.1:8080/boys
二、自定义的统一异常处理
虽然Spring Boot中实现了默认的error映射,但是在实际应用中,上面你的错误页面对用户来说并不够友好,我们通常需要去实现我们自己的异常提示。
1) 统一的异常处理类(com.dechy.handle)
@ControllerAdvice public class ExceptionHandle { private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class); @ExceptionHandler(value = Exception.class) @ResponseBody public Result handle(Exception e) { if (e instanceof BoyException) { BoyException boyException = (BoyException) e; return ResultUtil.error(boyException.getCode(), boyException.getMessage()); }else { logger.error("【系统异常】{}", e); return ResultUtil.error(-1, "未知错误"); } } }
2)自定义异常类(com.dechy.exception)
public class BoyException extends RuntimeException{ private Integer code; public BoyException(ResultEnum resultEnum) { super(resultEnum.getMsg()); this.code = resultEnum.getCode(); } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } }
3)返回结果枚举(com.dechy.enums)
public enum ResultEnum { UNKONW_ERROR(-1, "未知错误"), SUCCESS(0, "成功"), TOOSHORT(100, "身高太矮"), TOOHIGH(101, "身高太高"), ; private Integer code; private String msg; ResultEnum(Integer code, String msg) { this.code = code; this.msg = msg; } public Integer getCode() { return code; } public String getMsg() { return msg; } }
4)返回结果工具类(com.dechy.util)
public class ResultUtil { public static Result success(Object object) { Result result = new Result(); result.setCode(0); result.setMsg("成功"); result.setData(object); return result; } public static Result success() { return success(null); } public static Result error(Integer code, String msg) { Result result = new Result(); result.setCode(code); result.setMsg(msg); return result; } }
5)Boy实体类(com.dechy.model)
@Entity
public class Boy {
@Id
@GeneratedValue
private Integer id;
@NotBlank(message = "这个字段必传")
private String height;
@Min(value = 100, message = "体重必须大于100")
private BigDecimal weight;
public Integer getId (){
return id;
}
public void setId (Integer id){
this.id = id;
}
public String getHeight (){
return height;
}
public void setHeight (String height){
this.height = height;
}
public BigDecimal getWeight (){
return weight;
}
public void setWeight (BigDecimal weight){
this.weight = weight;
}
@Override
public String toString (){
return "Boy{" + "id=" + id + ", height=\'" + height + \'\\\'\' + ", weight=" + weight + \'}\';
}
}
6)业务层具体使用时抛出异常,等待统一处理(com.dechy.service)
public void chooseBoy(Integer id) throws Exception{ Boy boy= boyRepository.findOne(id); Integer height= boy.getHeight(); if (height < 100) { //返回"身高矮" code=100 throw new BoyException(ResultEnum.TOOSHORT); }else if (height > 200) { //返回"身高太高" code=101 throw new BoyException(ResultEnum.TOOHIGH); }//... }
以上是关于基于spring boot的统一异常处理的主要内容,如果未能解决你的问题,请参考以下文章