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 ...
随机推荐
- shell基础之99乘法表
方法一: 1 #!/bin/bash 2 for a in {1..9};do 3 for b in {1..9};do 4 c=`echo "$a*$b" |bc` 5 if [ ...
- 太赞了!Python竟可以轻松实现音频格式无损转换
大家好,我是辰哥 辰哥在平时处理音频格式的时候,需要去下载各种音频处理软件(专业一点的软件还要收费),掌握Python技术的我们,知道Python是万能的(哈哈哈,开个玩笑).今天辰哥就来教大家用Py ...
- 解决mysql无法远程连接的问题
前言 最近开发中遇到一个问题,mysql在服务器本地可以登录,但是远程通过3306端口却不可以.这个问题困扰了我一周之久,终于在今天解决了.在解决的过程中试了很多的方法,遂记录下来,希望能给大家一些提 ...
- Log4j实战,依赖分析
背景 在项目中经常被log4j的各种依赖冲突搞的焦头烂额,久病成良医啊,在这里记录一下我对log4j的理解与分析 log4j 与 log4j2 log4j2是log4j的升级版,二者互不兼容,据说lo ...
- Darknet_Yolov3模型搭建
Darknet_Yolov3模型搭建 YOLO(You only look once)是目前流行的目标检测模型之一,目前最新已经发展到V3版本了,在业界的应用也很广泛.YOLO的特点就是"快 ...
- 【逆向&渗透实战】Dump内存中的Dex_我是如何脱壳某公司加固过的Apk并利用其API渗透对方数据库
/作者:Kali_MG1937 QQ:3496925334 CNBLOG博客号:ALDYS4/ 某天午睡,朦朦胧胧梦到给学校提供建站模板的公司有个注射点 梦醒后,我凭借着零散的记忆日进了对面的数据库, ...
- Mysql数据库值的添加、修改、删除及清空
3.MySQL数据管理 第一种方式:不太建议,看起来复杂些 -- 学生表的 gradeid 字段,要去引用 年级表的 gradeid -- 定义外键key -- 给这个外键添加约束,(执行引用),RE ...
- Java持久层框架Mybatis入门
MyBatis是什么 MyBatis是Java的持久层框架,GitHub的star数高达15.8k,是Java技术栈中最热门的ORM框架之一.它支持自定义SQL.存储过程以及高级映射,可以通过XML或 ...
- 搭建DG(data guard),及搭建过程中遇到的一些小问题
一.准备工作:主库:虚拟机名称host06 主机名:host06 IP:192.168.100.60 db_name&SID:ENMO db_unique_name:PROD 存储:文件系统 ...
- 初学springboot
现在总是与数据库和前端打交道,让我觉得好厌烦,还是喜欢敲代码.最近问了几个朋友,都说潮流要学springCloud,然后学springCloud又要先学springboot,所以这段时间我会慢慢把sp ...