大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Spring中@RestControllerAdvice注解的原理是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
公司主营业务:网站建设、网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出海宁免费做网站回馈大家。
首先定义返回对象ResponseDto
package com.staff.points.common; import lombok.Data; import java.io.Serializable; @Data public class ResponseDtoimplements Serializable { private static final long serialVersionUID = -284719732991678911L; private String code; private String message; private T data; public static ResponseDto assemblingSuccessResponse(T data) { ResponseDto responseDto = new ResponseDto<>(); responseDto.setCode(ResponseCodeEnum.SUCCESS.getCode()); responseDto.setMessage(ResponseCodeEnum.SUCCESS.getMessage()); responseDto.setData(data); return responseDto; } public static ResponseDto assemblingSuccessResponse() { ResponseDto responseDto = new ResponseDto<>(); responseDto.setCode(ResponseCodeEnum.SUCCESS.getCode()); responseDto.setMessage(ResponseCodeEnum.SUCCESS.getMessage()); responseDto.setData(null); return responseDto; } public static ResponseDto assemblingFailureResponse(ResponseCodeEnum data) { ResponseDto responseDto = new ResponseDto<>(); responseDto.setCode(data.FAILURE.getCode()); responseDto.setMessage(data.FAILURE.getMessage()); return responseDto; } public static ResponseDto assemblingFailureResponse() { ResponseDto responseDto = new ResponseDto<>(); responseDto.setCode(ResponseCodeEnum.FAILURE.getCode()); responseDto.setMessage(ResponseCodeEnum.FAILURE.getMessage()); return responseDto; } }
然后定义返回码的枚举类,此处只定义了两种,有需要可以往下添加很多。
package com.staff.points.common; import lombok.AllArgsConstructor; import lombok.Getter; @AllArgsConstructor @Getter public enum ResponseCodeEnum { SUCCESS("00", "成功"), FAILURE("01", "系统异常"); private String code; private String message; }
下面是自定义的异常类
package com.staff.points.common; import lombok.Data; @Data public class StaffPointsException extends RuntimeException{ private String code; private String message; public StaffPointsException () {} public StaffPointsException (Exception e) { super(e); } public StaffPointsException (String code, String message) { super(message); this.code = code; this.message = message; } public StaffPointsException (ResponseCodeEnum codeEnum) { super(codeEnum.getMessage()); this.code = codeEnum.getCode(); this.message = codeEnum.getMessage(); } }
然后是关键的@RestControllerAdvice修饰的类
package com.staff.points.exception; import com.staff.points.common.ResponseCodeEnum; import com.staff.points.common.ResponseDto; import com.staff.points.common.StaffPointsException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @RestControllerAdvice @Component public class UnifyExceptionHandler { private Logger logger = LoggerFactory.getLogger(UnifyExceptionHandler.class); @ExceptionHandler(Exception.class) public ResponseDto handlerCommonException (Exception e) { ResponseDto responseDto = new ResponseDto<>(); responseDto.setCode(ResponseCodeEnum.FAILURE.getCode()); responseDto.setMessage(ResponseCodeEnum.FAILURE.getMessage()); logger.info("UnifyExceptionHandler.handlerCommonException exception:" + e); return responseDto; } // 报StaffPointException时,对其进行拦截并处理的方法 @ExceptionHandler(StaffPointsException.class) public ResponseDto handlerCustomizeException (StaffPointsException e) { ResponseDto responseDto = new ResponseDto<>(); responseDto.setCode(e.getCode()); responseDto.setMessage(e.getMessage()); logger.info("UnifyExceptionHandler.handlerCustomizeException StaffPointsException:" + e); return responseDto; } }
运行代码时,如果出现了StaffPointException,那么就会被拦截进入第27行的方法(就是说可以自由的在业务代码里往外throw自定义异常了);如果出现了其他的异常,则进入18行的方法,统一返回。
验证一下,在代码里造一个NPE异常时,返回结果:
{ "code": "01", "message": "系统异常", "data": null }
造一个StaffPointsException异常时,返回结果:
{ "code": "99", "message": "自定义业务异常", "data": null }
它的作用原理,大体是先在spring初始化时将类扫描进容器,出异常后,在DispatcherServlet类的doDispatch方法中调用了对异常的拦截处理。
。
关于Spring中@RestControllerAdvice注解的原理是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。