springboot 自定义异常
利用自定义异常,可以让我们做一些自定义操作,比如实现一个不需要写 return ,就能在我们设定好的逻辑处发生错误,就自动 return 出去,从而不需要写 if 判断,再 return 的写法,从而让代码看起来更加简洁,下面简单举个列子:
利用自定义异常,判断请求参数非空,若是空,就自动 return 出去:
1、定义一个自定义异常类,继承想继承的异常,这里继承运行时异常 RuntimeException【ResponseStatusEnum 枚举类和 ResultsBean 格式类可以看上一篇全局异常操作】
package com.sinosig.ewp.wx.service.config; /** * @author Mongo * @return */ public class MyCustomException extends RuntimeException{ private ResponseStatusEnum responseStatusEnum; public MyCustomException(ResponseStatusEnum responseStatusEnum,String msg) { responseStatusEnum.setMsg(msg); this.responseStatusEnum = responseStatusEnum; } public ResponseStatusEnum getResponseStatusEnum() { return responseStatusEnum; } public void setResponseStatusEnum(ResponseStatusEnum responseStatusEnum) { this.responseStatusEnum = responseStatusEnum; } }
2、在全局异常类中引入:
package com.sinosig.ewp.wx.service.config; import com.sinosig.ewp.common.base.ResultDTO; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestControllerAdvice; /** * @author Mongo * @return */ @RestControllerAdvice @Slf4j public class GraceExceptionHandler { /** * 全局异常捕获 * @param e * @return */ @ExceptionHandler(Exception.class) @ResponseBody public Object returnException(Exception e) { log.error("error",e); return ResultsBean.build(500,"全局异常啦啦啦"); } /** * 自定义验证参数非空异常 * @param e * @return */ @ExceptionHandler(MyCustomException.class) @ResponseBody public Object returnMyCustomException(MyCustomException e) { log.info("异常为:"+e.getMessage()); return ResultsBean.exception(e.getResponseStatusEnum()); } }
3、参数非空判断工具类
package com.sinosig.ewp.wx.service.config; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; /** * @author Mongo * @return */ @Component @Slf4j public class ParamUtils { /** * 字符串类型参数非空判断 * param 要判断的字符串字段 * msg 若为空要提示的文字 */ public static String nullString(String param, String msg){ if(StringUtils.isBlank(param)){ throw new MyCustomException(ResponseStatusEnum.PARAM_NULL,msg); } return param; } /** * 任意类型参数非空判断 * param 要判断的任意字段 * o 想返回的参数类型 * msg 若为空要提示的文字 */ public static <T> T nullParam(Object param,Class<T> o, String msg){ if(param == null){ throw new MyCustomException(ResponseStatusEnum.PARAM_NULL,msg); } if(param instanceof String){ if(StringUtils.isBlank(String.valueOf(param))){ throw new MyCustomException(ResponseStatusEnum.PARAM_NULL,msg); } } return (T) param; } }
4、最后测试类
package com.sinosig.ewp.wx.service.controller; import com.sinosig.ewp.wx.service.config.ParamUtils; import com.sinosig.ewp.wx.service.config.ResultsBean; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; /** * @author Mongo * @return */ @Slf4j @CrossOrigin @RestController public class TestsController { @PostMapping(value = "/demo1") public ResultsBean demo1(@RequestBody ParamBean paramBean){ System.out.println("==开始测试=="); //这里 id 和 name 不再需要 if 判断了,如果为空,会自动 return 出去。 Integer id = ParamUtils.nullParam(paramBean.getId(), Integer.class, "请求 id 不能为空"); String name = ParamUtils.nullString(paramBean.getName(), "请求姓名不能为空"); System.out.println("id = " + id); System.out.println("name = " + name); System.out.println("==结束测试=="); return ResultsBean.ok(); } }
结果如下:
(1)正常都有数据时:
(2)id 没有数据时:
(3)name 没有数据时: