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
spring 5 webflux异常处理的更多相关文章
- Spring Boot WebFlux 2.1.7 中文翻译文档
1. 前言 从一开始学习 Netty 到 rxjava.Rector,再到 java8 的 CompletableFuture,就深深的为响应式编程着迷,这种区别于传统的顺序式编程,没准未来能在编程世 ...
- spring mvc4:异常处理
前面学习过struts2的异常处理,今天来看下spring mvc4的异常处理: 一.Servlet配置文件修改 <bean id="exceptionResolver" c ...
- 使用Spring MVC统一异常处理实战
1 描述 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合 ...
- Spring MVC 统一异常处理
Spring MVC 统一异常处理 看到 Exception 这个单词都心慌 如果有一天你发现好久没有看到Exception这个单词了,那你会不会想念她?我是不会的.她如女孩一样的令人心动又心慌,又或 ...
- Spring 5 WebFlux
作者: 一字马胡 转载标志 [2017-11-26] 更新日志 日期 更新内容 备注 2017-11-26 新建文章 Spring 5 WebFlux demo Reactor Spring 5的一大 ...
- 【Spring学习笔记-MVC-15.1】Spring MVC之异常处理=404界面
作者:ssslinppp 异常处理请参考前篇博客:<[Spring学习笔记-MVC-15]Spring MVC之异常处理>http://www.cnblogs.com/sssl ...
- 使用Spring MVC统一异常处理实战<转>
1 描述 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合 ...
- Spring Boot WebFlux 快速入门实践
02:WebFlux 快速入门实践 Spring Boot 2.0 spring.io 官网有句醒目的话是: BUILD ANYTHING WITH SPRING BOOT Spring Boot ( ...
- Spring Boot WebFlux 增删改查完整实战 demo
03:WebFlux Web CRUD 实践 前言 上一篇基于功能性端点去创建一个简单服务,实现了 Hello .这一篇用 Spring Boot WebFlux 的注解控制层技术创建一个 CRUD ...
随机推荐
- Linux进阶之正则,shell三剑客(grep,awk,sed),cut,sort,uniq
一.正则表达式:Regular Expression 正则表达式:正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串.在很多文本编辑器里,正则表达式通常被用来检索.替换那些符合某个模式 ...
- mysql基础之mariadb对表中数据的增删改查
复习: 查看表:show tables; 创建表:create table 表名(字符类型); 删除表:drop table 表名; 对表的结构进行增删改查: 查看表结构:desc 表名; 修改表-添 ...
- 【python接口自动化】初识unittest框架
本文将介绍单元测试的基础版及使用unittest框架的单元测试. 完成以下需求的代码编写,并实现单元测试 账号正确,密码正确,返回{"msg":"账号密码正确,登录成功& ...
- Psexec和wmiexec的原理和区别
PSEXEC 针对远程建立连接的方式有两种,一种先建立IPC通道连接,然后直接使用,操作如下: net use \\192.168.0.1\ipc$ "password" /use ...
- GO学习-(18) Go语言基础之并发
Go语言基础之并发 并发是编程里面一个非常重要的概念,Go语言在语言层面天生支持并发,这也是Go语言流行的一个很重要的原因. Go语言中的并发编程 并发与并行 并发:同一时间段内执行多个任务(你在用微 ...
- Go基础结构与类型06---房贷计算器
package main import ( "fmt" "math" "strconv" ) /* 输入的金额.年化利息(0.05代表5%) ...
- 内部排序算法(交换排序,插入排序)注意点(C语言实现)
对于算法思想的理解可以参考下面的这个帖子,十大经典排序算法(动图演示) - 一像素 - 博客园,因为算法的逻辑和数学很像,相应的基础资料一般也能在网上找到,所以,本帖子这谈论一些重要的注意点,其他人讲 ...
- .NET Core Web API使用HttpClient提交文件的二进制流(multipart/form-data内容类型)
需求背景: 在需要通过服务端请求传递文件二进制文件流数据到相关的服务端保存时,如对接第三方接口很多情况下都会提供一个上传文件的接口,但是当你直接通过前端Ajax的方式将文件流上传到对方提供的接口的时候 ...
- JavaScript 中精度问题以及解决方案
JavaScript 中的数字按照 IEEE 754 的标准,使用 64 位双精度浮点型来表示.其中符号位 S,指数位 E,尾数位M分别占了 1,11,52 位,并且在 ES5 规范 中指出了指数位E ...
- 前台JS遍历Table的所有单元格数据内容
在日常开发过程中为了减少与后台服务器的交互次数,大部分的功能都会放到前台使用JS来完成. 例如:一个表中有ID(FOCUS_SEQ)和Name(COLUMNCTRL)两个字段,其中ID是自定义连续增长 ...