在现在的前后端交互中,通常都规范了接口返回方式,如返回的接口状态(成功|失败)以及要返回的数据在那个字段取,或者说失败了以后提示信息从接口哪里返回,因此,如果想做全局异常,并且异常发生后能准确的返回给前端解析,那么需要异常发生时返回给前端的格式与正常失败场景的格式一致。
利用idea 工具,很容易的搭建一个SpringBoot项目,要引入的maven依赖如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
很简单,除了加入web功能还加入了我们需要用到的JSR-303校验框架。
public class Code { /** * 成功 */ public static int SUCCESSED = 1; /** * 失败 */ public static int FAILED = -1; }
public class Response<T> implements Serializable{ /** * */ private static final long serialVersionUID = 4250719891313555820L; /** * 返回结果集 */ private T result; /** * 返回消息 */ private String msg; /** * 响应码 */ private Integer code; //set get 略 }
public class MyException extends RuntimeException { private static final long serialVersionUID = -5875371379845226068L; public MyException(){} public MyException(String msg){ this.msg = msg ; } /** * 异常信息 */ private String msg ; /** * 具体异常码 */ private int code = Code.FAILED; get set 略
@ControllerAdvice public class GlobalExceptionHandler { private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); @ExceptionHandler(value = MyException.class) @ResponseBody public Response<String> myExceptionErrorHandler(MyException ex) throws Exception { logger.error("myExceptionErrorHandler info:{}",ex.getMessage()); Response<String> r = new Response<>(); r.setMsg(ex.getMsg()); r.setCode(ex.getCode()); return r; }
@RestController @RequestMapping("/user") public class UserController { @PostMapping(value = "/update") Response<Boolean> update(User user){ //todo 此处为模拟异常抛出 if(true){ throw new MyException("更新失败"); } //todo 此处为模拟返回 Response<Boolean> response = new Response<>(); response.setCode(Code.SUCCESSED); response.setResult(true); return response; } }
通常我们操作数据的时候,不仅前端需要进行数据校验,后端也应当进行拦截和进行相应的错误提示,JSR-303校验框架也是我们的一种选择。
public class User { @NotNull(message = "用户名不能为空") private String userName; private int age; //...
@ControllerAdvice public class GlobalExceptionHandler { private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); @ExceptionHandler(value = BindException.class) @ResponseBody public Response<String> bindExceptionErrorHandler(BindException ex) throws Exception { logger.error("bindExceptionErrorHandler info:{}",ex.getMessage()); Response<String> r = new Response<>(); StringBuilder sb = new StringBuilder(); FieldError fieldError = ex.getFieldError(); sb.append(fieldError.getDefaultMessage()); r.setMsg(sb.toString()); r.setCode(Code.FAILED); return r; } //...
@RestController @RequestMapping("/user") public class UserController { @PostMapping(value = "/add") Response<User> add(@Validated User user){ //todo 此处为模拟返回 Response<User> response = new Response<>(); response.setCode(Code.SUCCESSED); response.setResult(new User()); return response; } //...
不填写任何属性,模拟添加操作,准确进行拦截和报错
适合的才是最好的,每个团队都应摸索出自己的一套异常解决方案,本文所提仅针对业务异常,希望大家也能有所收获
【责任编辑:庞桂玉 TEL:(010)68476606】