Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 507|回复: 0

Spring Boot 2 Webflux的全局异常处理

[复制链接]
  • TA的每日心情
    奋斗
    前天 11:45
  • 签到天数: 757 天

    [LV.10]以坛为家III

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    707886
    发表于 2021-7-16 21:04:20 | 显示全部楼层 |阅读模式

    https://www.jianshu.com/p/6f631f3e00b9

     

    本文首先将会回顾Spring 5之前的SpringMVC异常处理机制,然后主要讲解Spring Boot 2 Webflux的全局异常处理机制。

    SpringMVC的异常处理

    Spring 统一异常处理有 3 种方式,分别为:

    • 使用 @ExceptionHandler 注解
    • 实现 HandlerExceptionResolver 接口
    • 使用 @controlleradvice 注解

    使用@ExceptionHandler注解

    用于局部方法捕获,与抛出异常的方法处于同一个Controller类:

    @Controller public class BuzController { @ExceptionHandler({NullPointerException.class}) public String exception(NullPointerException e) { System.out.println(e.getMessage()); e.printStackTrace(); return "null pointer exception"; } @RequestMapping("test") public void test() { throw new NullPointerException("出错了!"); } } 

    如上的代码实现,针对BuzController抛出的NullPointerException异常,将会捕获局部异常,返回指定的内容。

    实现HandlerExceptionResolver接口

    通过实现HandlerExceptionResolver接口,这里我们通过继承SimpleMappingExceptionResolver实现类(HandlerExceptionResolver实现,允许将异常类名称映射到视图名称,既可以是一组给定的handlers处理程序,也可以是DispatcherServlet中的所有handlers)定义全局异常:

    @Component public class CustomMvcExceptionHandler extends SimpleMappingExceptionResolver { private ObjectMapper objectMapper; public CustomMvcExceptionHandler() { objectMapper = new ObjectMapper(); } @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception ex) { response.setStatus(200); response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-cache, must-revalidate"); Map<String, Object> map = new HashMap<>(); if (ex instanceof NullPointerException) { map.put("code", ResponseCode.NP_EXCEPTION); } else if (ex instanceof IndexOutOfBoundsException) { map.put("code", ResponseCode.INDEX_OUT_OF_BOUNDS_EXCEPTION); } else { map.put("code", ResponseCode.CATCH_EXCEPTION); } try { map.put("data", ex.getMessage()); response.getWriter().write(objectMapper.writeValueAsString(map)); } catch (Exception e) { e.printStackTrace(); } return new ModelAndView(); } } 

    如上为示例的使用方式,我们可以根据各种异常定制错误的响应。

    使用@controlleradvice注解

    @ControllerAdvice public class ExceptionController { @ExceptionHandler(RuntimeException.class) public ModelAndView handlerRuntimeException(RuntimeException ex) { if (ex instanceof MaxUploadSizeExceededException) { return new ModelAndView("error").addObject("msg", "文件太大!"); } return new ModelAndView("error").addObject("msg", "未知错误:" + ex); } @ExceptionHandler(Exception.class) public ModelAndView handlerMaxUploadSizeExceededException(Exception ex) { if (ex != null) { return new ModelAndView("error").addObject("msg", ex); } return new ModelAndView("error").addObject("msg", "未知错误:" + ex); } } 

    和第一种方式的区别在于,ExceptionHandler的定义和异常捕获可以扩展到全局。

    Spring 5 Webflux的异常处理

    webflux支持mvc的注解,是一个非常便利的功能,相比较于RouteFunction,自动扫描注册比较省事。异常处理可以沿用ExceptionHandler。如下的全局异常处理对于RestController依然生效。

    @RestControllerAdvice public class CustomExceptionHandler { private final Log logger = LogFactory.getLog(getClass()); @ExceptionHandler(Exception.class) @ResponseStatus(code = HttpStatus.OK) public ErrorCode handleCustomException(Exception e) { logger.error(e.getMessage()); return new ErrorCode("e","error" ); } } 

    WebFlux示例

    WebFlux提供了一套函数式接口,可以用来实现类似MVC的效果。我们先接触两个常用的。

    Controller定义对Request的处理逻辑的方式,主要有方面:

    • 方法定义处理逻辑;
    • 然后用@RequestMapping注解定义好这个方法对什么样url进行响应。

    在WebFlux的函数式开发模式中,我们用HandlerFunction和RouterFunction来实现上边这两点。

    HandlerFunction

    HandlerFunction相当于Controller中的具体处理方法,输入为请求,输出为装在Mono中的响应:

        Mono<T> handle(ServerRequest var1); 

    在WebFlux中,请求和响应不再是WebMVC中的ServletRequest和ServletResponse,而是ServerRequest和ServerResponse。后者是在响应式编程中使用的接口,它们提供了对非阻塞和回压特性的支持,以及Http消息体与响应式类型Mono和Flux的转换方法。

    @Component public class TimeHandler { public Mono<ServerResponse> getTime(ServerRequest serverRequest) { String timeType = serverRequest.queryParam("type").get(); //return ... } } 

    如上定义了一个TimeHandler,根据请求的参数返回当前时间。

    RouterFunction

    RouterFunction,顾名思义,路由,相当于@RequestMapping,用来判断什么样的url映射到那个具体的HandlerFunction。输入为请求,输出为Mono中的Handlerfunction

    Mono<HandlerFunction<T>> route(ServerRequest var1); 

    针对我们要对外提供的功能,我们定义一个Route。

    @Configuration public class RouterConfig { private final TimeHandler timeHandler; @Autowired public RouterConfig(TimeHandler timeHandler) { this.timeHandler = timeHandler; } @Bean public RouterFunction<ServerResponse> timerRouter() { return route(GET("/time"), req -> timeHandler.getTime(req)); } } 

    可以看到访问/time的GET请求,将会由TimeHandler::getTime处理。

    功能级别处理异常

    如果我们在没有指定时间类型(type)的情况下调用相同的请求地址,例如/time,它将抛出异常。
    Mono和Flux APIs内置了两个关键操作符,用于处理功能级别上的错误。

    使用onErrorResume处理错误

    还可以使用onErrorResume处理错误,fallback方法定义如下:

    Mono<T> onErrorResume(Function<? super Throwable, ? extends Mono<? extends T>> fallback); 

    当出现错误时,我们使用fallback方法执行替代路径:

    @Component public class TimeHandler { public Mono<ServerResponse> getTime(ServerRequest serverRequest) { String timeType = serverRequest.queryParam("time").orElse("Now"); return getTimeByType(timeType).flatMap(s -> ServerResponse.ok() .contentType(MediaType.TEXT_PLAIN).syncBody(s)) .onErrorResume(e -> Mono.just("Error: " + e.getMessage()).flatMap(s -> ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).syncBody(s))); } private Mono<String> getTimeByType(String timeType) { String type = Optional.ofNullable(timeType).orElse( "Now" ); switch (type) { case "Now": return Mono.just("Now is " + new SimpleDateFormat("HH:mm:ss").format(new Date())); case "Today": return Mono.just("Today is " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())); default: return Mono.empty(); } } } 

    在如上的实现中,每当getTimeByType()抛出异常时,将会执行我们定义的fallback方法。除此之外,我们还可以捕获、包装和重新抛出异常,例如作为自定义业务异常:

        public Mono<ServerResponse> getTime(ServerRequest serverRequest) { String timeType = serverRequest.queryParam("time").orElse("Now"); return ServerResponse.ok() .body(getTimeByType(timeType) .onErrorResume(e -> Mono.error(new ServerException(new ErrorCode(HttpStatus.BAD_REQUEST.value(), "timeType is required", e.getMessage())))), String.class); } 

    使用onErrorReturn处理错误

    每当发生错误时,我们可以使用onErrorReturn()返回静态默认值:

        public Mono<ServerResponse> getDate(ServerRequest serverRequest) { String timeType = serverRequest.queryParam("time").get(); return getTimeByType(timeType) .onErrorReturn("Today is " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())) .flatMap(s -> ServerResponse.ok() .contentType(MediaType.TEXT_PLAIN).syncBody(s)); } 

    全局异常处理

    如上的配置是在方法的级别处理异常,如同对注解的Controller全局异常处理一样,WebFlux的函数式开发模式也可以进行全局异常处理。要做到这一点,我们只需要自定义全局错误响应属性,并且实现全局错误处理逻辑。

    我们的处理程序抛出的异常将自动转换为HTTP状态和JSON错误正文。要自定义这些,我们可以简单地扩展DefaultErrorAttributes类并覆盖其getErrorAttributes()方法:

    @Component public class GlobalErrorAttributes extends DefaultErrorAttributes { public GlobalErrorAttributes() { super(false); } @Override public Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) { return assembleError(request); } private Map<String, Object> assembleError(ServerRequest request) { Map<String, Object> errorAttributes = new LinkedHashMap<>(); Throwable error = getError(request); if (error instanceof ServerException) { errorAttributes.put("code", ((ServerException) error).getCode().getCode()); errorAttributes.put("data", error.getMessage()); } else { errorAttributes.put("code", HttpStatus.INTERNAL_SERVER_ERROR); errorAttributes.put("data", "INTERNAL SERVER ERROR"); } return errorAttributes; } //...有省略 } 

    如上的实现中,我们对ServerException进行了特别处理,根据传入的ErrorCode对象构造对应的响应。

    接下来,让我们实现全局错误处理程序。为此,Spring提供了一个方便的AbstractErrorWebExceptionHandler类,供我们在处理全局错误时进行扩展和实现:

    @Component @Order(-2) public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler { //构造函数 @Override protected RouterFunction<ServerResponse> getRoutingFunction(final ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse); } private Mono<ServerResponse> renderErrorResponse(final ServerRequest request) { final Map<String, Object> errorPropertiesMap = getErrorAttributes(request, true); return ServerResponse.status(HttpStatus.OK) .contentType(MediaType.APPLICATION_JSON_UTF8) .body(BodyInserters.fromObject(errorPropertiesMap)); } } 

    这里将全局错误处理程序的顺序设置为-2。这是为了让它比@Order(-1)注册的DefaultErrorWebExceptionHandler处理程序更高的优先级。

    该errorAttributes对象将是我们在网络异常处理程序的构造函数传递一个的精确副本。理想情况下,这应该是我们自定义的Error Attributes类。然后,我们清楚地表明我们想要将所有错误处理请求路由到renderErrorResponse()方法。最后,我们获取错误属性并将它们插入服务器响应主体中。

    然后,它会生成一个JSON响应,其中包含错误,HTTP状态和计算机客户端异常消息的详细信息。对于浏览器客户端,它有一个whitelabel错误处理程序,它以HTML格式呈现相同的数据。当然,这可以是定制的。

    小结

    本文首先讲了Spring 5之前的SpringMVC异常处理机制,SpringMVC统一异常处理有 3 种方式:使用 @ExceptionHandler 注解、实现 HandlerExceptionResolver 接口、使用 @controlleradvice 注解;然后通过WebFlux的函数式接口构建Web应用,讲解Spring Boot 2 Webflux的函数级别和全局异常处理机制(对于Spring WebMVC风格,基于注解的方式编写响应式的Web服务,仍然可以通过SpringMVC统一异常处理实现)。

    注:本文后半部分基本翻译自https://www.baeldung.com/spring-webflux-errors

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-7-4 13:39 , Processed in 0.068463 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表