本文主要研究一下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

spring 5 webflux异常处理的更多相关文章

  1. Spring Boot WebFlux 2.1.7 中文翻译文档

    1. 前言 从一开始学习 Netty 到 rxjava.Rector,再到 java8 的 CompletableFuture,就深深的为响应式编程着迷,这种区别于传统的顺序式编程,没准未来能在编程世 ...

  2. spring mvc4:异常处理

    前面学习过struts2的异常处理,今天来看下spring mvc4的异常处理: 一.Servlet配置文件修改 <bean id="exceptionResolver" c ...

  3. 使用Spring MVC统一异常处理实战

    1 描述 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合 ...

  4. Spring MVC 统一异常处理

    Spring MVC 统一异常处理 看到 Exception 这个单词都心慌 如果有一天你发现好久没有看到Exception这个单词了,那你会不会想念她?我是不会的.她如女孩一样的令人心动又心慌,又或 ...

  5. Spring 5 WebFlux

    作者: 一字马胡 转载标志 [2017-11-26] 更新日志 日期 更新内容 备注 2017-11-26 新建文章 Spring 5 WebFlux demo Reactor Spring 5的一大 ...

  6. 【Spring学习笔记-MVC-15.1】Spring MVC之异常处理=404界面

    作者:ssslinppp       异常处理请参考前篇博客:<[Spring学习笔记-MVC-15]Spring MVC之异常处理>http://www.cnblogs.com/sssl ...

  7. 使用Spring MVC统一异常处理实战<转>

    1 描述 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合 ...

  8. Spring Boot WebFlux 快速入门实践

    02:WebFlux 快速入门实践 Spring Boot 2.0 spring.io 官网有句醒目的话是: BUILD ANYTHING WITH SPRING BOOT Spring Boot ( ...

  9. Spring Boot WebFlux 增删改查完整实战 demo

    03:WebFlux Web CRUD 实践 前言 上一篇基于功能性端点去创建一个简单服务,实现了 Hello .这一篇用 Spring Boot WebFlux 的注解控制层技术创建一个 CRUD ...

随机推荐

  1. 控制器网关/dns设置

    如果控制器ping内网可以,但是ping不同外网,十有八九是因为网关的问题,可以使用route命令设置网关,如设置为192.168.31.1(不是192.168.31.0),route add def ...

  2. centos 7修改yum源

    centos系统要定期更新,前天使用sudo yum update命令更新过程中出错,安装的是x64的系统,结果更新的内容有i686的依赖包,最终由于64与32位系统依赖的原因导致更新失败,更糟糕的是 ...

  3. Centos7 vsftpd虚拟用户权限控制(vsftpd虚拟用户限制IP访问)

    实验目标:(新增限制虚拟机用户登陆的IP) 实现在同一跟目录下对admin,upload,download三个虚拟用户的不同权限的控制.具体权限控制列表如下: 用户名 权限说明 admin 管理员,可 ...

  4. 阿里云RDs 网络白名单 专用网络 经典网络

    云服务为了安全性总是有不同的安全规则,第一就是需要明白里面的网络类型   专有网络是您自己独有的云上私有网络.您可以完全掌控自己的专有网络,例如选择IP地址范围.配置路由表和网关等,您可以在自己定义的 ...

  5. Spring Cloud 升级之路 - 2020.0.x - 7. 使用 Spring Cloud LoadBalancer (2)

    本项目代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford 我们使用 Spri ...

  6. Google Hacking 搜索引擎攻击与防范

    Google Hacking,有时也会被称为 Google dorking,是一种利用谷歌搜索的高级使用方式进行信息收集的技术.这个概念最早在2000年由黑客 Johnny Long 提出并推广,一系 ...

  7. Stopper的使用

    工具类,抽象死循环逻辑的 import java.util.concurrent.atomic.AtomicBoolean; /** * if the process closes, a signal ...

  8. TensorFlow XLA加速编译器

    TensorFlow XLA加速编译器 加速线性代数器(Accelerated linear algebra,XLA)是线性代数领域的专用编译器.根据 https://www.tensorflow.o ...

  9. MySQL泛泛而谈(3W字)

    下面对于MySQL进行相关介绍,文档的内容较为基础,仅仅设计操作,少量原理,大佬请绕道哦. 废话少说,开冲! 一.MySQL架构介绍 1-MySQL简介 概述 MySQL是一个关系型数据库管理系统,由 ...

  10. Python_Selenium 之PO模式的思想、优化思路

    一.PO模式思想 PO模式是一种自动化测试设计模式,将页面定位和业务操作分开,也就是把对象的定位和测试脚本分开,从而提供可维护性. PO设计模式基础(页面作为类.元素对象作为属性.元素操作作为方法) ...