SpringBoot全局异常处理
说明:本文是基于SpringBoot的全局异常处理,主要提现在返回的结果集
技术:java自定义异常,SpringBoot日志打印,全局异常
需求:模拟 请求参数为1时,后台抛异常:等于1啦 同时请求响应返回Postman对应的请求结果集,控制台打印异常
类说明如图:
接下来就开始代码了:
控制层 TestController
package com.example.dev.controller; import com.example.dev.exception.MyException; import com.example.dev.service.TestService; import com.example.dev.utils.result.Result; import com.example.dev.utils.result.ResultUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("test") public class TestController { @Autowired private TestService testService; /** * 测试全局异常 * @param val * @return * @throws MyException */ @RequestMapping("/hello") @ResponseBody public Result hello (String val) throws MyException { testService.getWhatDo(val); return ResultUtil.success(); } }
业务层接口 TestService
package com.example.dev.service; import com.example.dev.exception.MyException; public interface TestService { /** * 测试全局异常 * @param val * @throws MyException */ public void getWhatDo(String val) throws MyException; }
业务层实现类 TestServiceImpl 枚举这里就不加了
package com.example.dev.service.impl; import com.example.dev.exception.MyException; import com.example.dev.service.TestService; import com.example.dev.utils.enums.ResEnum; import org.springframework.stereotype.Service; @Service public class TestServiceImpl implements TestService { /** * 测试全局异常 * @param val * @throws MyException */ public void getWhatDo(String val) throws MyException{ if(val.equals("1")){ throw new MyException("1","等于1啦"); }else if(val.equals("2")){ throw new MyException("2","等于2啦"); }else if(val.equals("0")){ int a = 1/0; } //如果整合枚举 /*System.out.println(ResEnum.EQUALONE.getIndex()); if(val.equals(ResEnum.EQUALONE.getIndex())){ throw new MyException(ResEnum.EQUALONE.getIndex(),ResEnum.EQUALONE.getName()); }else if(val.equals(ResEnum.EQUALTWO.getIndex())){ throw new MyException(ResEnum.EQUALTWO.getIndex(),ResEnum.EQUALTWO.getName()); }else if(val.equals("0")){ int a = 1/0; }*/ } }
结果集 Result
package com.example.dev.utils.result; import lombok.Data; @Data public class Result { private String code; private Object date; private String msg; }
结果集工具类 ResultUtil
package com.example.dev.utils.result; public class ResultUtil { /**返回成功 */ public static Result success(){ Result result = new Result(); result.setCode("0");//成功 result.setMsg("成功");//提示语 return result; } /**返回成功 */ public static Result success(Object object){ Result result = new Result(); result.setCode("0");//成功 result.setMsg("成功");//提示语 result.setDate(object);//返回内容 return result; } /**返回失败 */ public static Result error(){ Result result = new Result(); result.setCode("1");//失败 result.setMsg("失败");//提示语 return result; } /**返回失败 */ public static Result error(String code, String msg){ Result result = new Result(); result.setCode(code);//失败 result.setMsg(msg);//提示语 return result; } }
自定义异常 MyException
package com.example.dev.exception; public class MyException extends RuntimeException{ private String code; private String msg; public MyException(String code, String msg){ super(msg); this.code = code; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
全局异常处理
package com.example.dev.exception; import com.example.dev.utils.result.Result; import com.example.dev.utils.result.ResultUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public class TopException { private Logger logger = LoggerFactory.getLogger(this.getClass()); @ExceptionHandler(value = Exception.class) @ResponseBody public Result handle(Exception e){ if(e instanceof MyException){ logger.error("业务日志",e); MyException myException = (MyException) e; return ResultUtil.error(myException.getCode(),myException.getMessage()); } logger.error("系统日志",e); return ResultUtil.error("1000","业务繁忙"); } }
接下来是Postman测试,控制台打印
到此,全局异常处理就完成了。
处于代码的维护,TestServiceImpl 中 getWhatDo 方法体中,把没注的代码全部注掉,注掉的代码放开,加入枚举
枚举 ResEnum 当然我的枚举是随便定义的,你可以合理定义一下
package com.example.dev.utils.enums; public enum ResEnum { EQUALONE("等于1啦","1"),EQUALTWO("等于2啦","2"); private String name; private String index; private ResEnum(String name, String index) { this.index = index; this.name = name; } public static String getName(String index) { for (ResEnum c : ResEnum.values()) { if (index.equals(c.getIndex())) { return c.name; } } return null; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIndex() { return index; } public void setIndex(String index) { this.index = index; } }
这样SpringBoot的全局异常处理+返回结果集就完成了。。。。。。
每天进步一点点。。。。。。。。