• spring 5 webflux异常处理


    本文主要研究一下spring 5 webflux的异常处理

    maven

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-webflux</artifactId>
            </dependency>
    

    ExceptionHandler

    由于webflux也支持传统spring mvc的大部分注解,因此原来的ExceptionHandler也是支持的。

    @RestControllerAdvice
    public class ExceptionHandlers {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandlers.class);
    
        @ExceptionHandler(Exception.class)
        @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
        public String serverExceptionHandler(Exception ex) {
            LOGGER.error(ex.getMessage(),ex);
            return ex.getMessage();
        }
    }
    

    启动的时候可以看到日志

    2018-02-12 19:26:03.249  INFO 7053 --- [           main] o.s.w.r.r.m.a.ControllerMethodResolver   : Looking for @ControllerAdvice: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@65d09a04: startup date [Mon Feb 12 19:25:59 CST 2018]; root of context hierarchy
    2018-02-12 19:26:03.281  INFO 7053 --- [           main] o.s.w.r.r.m.a.ControllerMethodResolver   : Detected @ExceptionHandler methods in exceptionHandlers
    

    spring-webflux-5.0.2.RELEASE-sources.jar!/org/springframework/web/reactive/result/method/annotation/ControllerMethodResolver.java

    /**
     * Package-private class to assist {@link RequestMappingHandlerAdapter} with
     * resolving, initializing, and caching annotated methods declared in
     * {@code @Controller} and {@code @ControllerAdvice} components:
     * <ul>
     * <li>{@code @InitBinder}
     * <li>{@code @ModelAttribute}
     * <li>{@code @RequestMapping}
     * <li>{@code @ExceptionHandler}
     * </ul>
     *
     * @author Rossen Stoyanchev
     * @since 5.0
     */
    class ControllerMethodResolver {
    
        private static Log logger = LogFactory.getLog(ControllerMethodResolver.class);
    
    
        private final List<SyncHandlerMethodArgumentResolver> initBinderResolvers;
    
        private final List<HandlerMethodArgumentResolver> modelAttributeResolvers;
    
        private final List<HandlerMethodArgumentResolver> requestMappingResolvers;
    
        private final List<HandlerMethodArgumentResolver> exceptionHandlerResolvers;
    
        private final ReactiveAdapterRegistry reactiveAdapterRegistry;
        //......
    }
    

    可以看到支持InitBinder,ModelAttribute,RequestMapping,ExceptionHandler这几个注解。

    实例

        @GetMapping(value = "/error",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
        public Mono<String> exceptionReturn(){
    //        throw new RuntimeException("hello");
            return Mono.error(new RuntimeException("test error"));
        }
    

    与传统mvc不同的是,除了直接throw异常外,Mono或Flux可以直接error一个异常,在exceptionHandlers都可以被接收处理

    小结

    webflux支持mvc的注解,是一个非常便利的功能,相比较于RouteFunction,自动扫描注册比较省事。异常处理可以沿用ExceptionHandler。



    作者:go4it
    转自:https://www.jianshu.com/p/ca89ea42a327

  • 相关阅读:
    CSS3详解:transform
    js解决checkbox全选和反选的问题
    Scroll文字滚动js
    PAT乙级 解题目录
    PAT 1005 继续(3n+1)猜想 (25)(代码)
    PAT 1004 成绩排名 (20)(代码)
    PAT 1002 写出这个数 (20)(代码)
    PAT 1001 害死人不偿命的(3n+1)猜想 (15)(C++&JAVA&Python)
    PAT 1045 快速排序(25)(STL-set+思路+测试点分析)
    PAT 1050 螺旋矩阵(25)(代码)
  • 原文地址:https://www.cnblogs.com/dauber/p/9210526.html
Copyright © 2020-2023  润新知