webflux 并不算一门新技术,它出自 Spring,所以 Spring 中的一些特性它都有,包括 SpringMVC 中的一些注解它也可以沿用。没看过 webflux 教程的,可以参考我前面关于 webflux 教程的一些文章。本文我们来学习 webflux 中两个比较特独的方法 onErrorResume 和 onErrorReturn。
先回忆一下,我们在 SpringMVC 或者 SpringBoot 中常用的处理异常的方式有:@RestControllerAdvice、@ExceptionHandler、@ResponseStatus、HandlerExceptionResolver、@controlleradvice 等来实现。在 webflux 中这些也都可以使用,但是我不推荐这样使用,因为这样使用就破坏了 WebFlux 应有的特性!
onErrorResume
在 webflux 中,我们可以通过 onErrorResume 来处理功能基本的异常。onErrorResume 通过回掉的方式进行处理,整个 webflux 是一种链式调用。onErrorResume 的源码如下:
Mono<T> onErrorResume(Function<? super Throwable, ? extends Mono<? extends T>> fallback);
下面我们来演示一下 onErrorResume 的用法。新建项目之类的,我就不说了,不懂的看前面的文章!我们直接上核心代码。
@Configuration
public class XttblogRouter {
@Autowired
private XttblogHandler handler;
@Bean
public RouterFunction<ServerResponse> xttblog() {
return route(GET("/xttblog"), req -> handler.xttblog(req));
}
}
XttblogHandler 的实现代码如下:
@Component
public class XttblogHandler {
public Mono<ServerResponse> xttblog(ServerRequest serverRequest) {
String param = serverRequest.queryParam("test").orElse("other");
return toDoTest(param).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> toDoTest(String param) {
String type = Optional.ofNullable(param).orElse(
"other"
);
switch (type) {
case "xttblog":
return Mono.just("www.xttblog.com");
case "codedq":
return Mono.just("www.codedq.net");
case "other":
throw new NullPointerException("我抛出一个空异常了");
default:
return Mono.just(type);
}
}
}
然后通过 SpringBoot 启动项目,分别试试不同的参数进行访问。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
当是 localhost:8080/xttblog?test=other 这个的时候,就会打印异常信息。并返回 500 错误信息。
onErrorReturn
参考前面的 onErrorResume,onErrorReturn 也就显的非常的简单。每当发生错误时,我们可以使用 onErrorReturn() 返回静态默认值:
public Mono<ServerResponse> test(ServerRequest serverRequest) {
String param = serverRequest.queryParam("test").get();
return toDoTest(param)
.onErrorReturn("onErrorReturn .... www.xttblog.com")
.flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN).syncBody(s));
}
onErrorReturn 和 onErrorResume 有什么区别吗?
从源码上开它们的区别不大,只不过 onErrorReturn 的范围更大一些而已。onErrorReturn 返回一个静态值,onErrorResume 返回一个动态值,并且可以捕获,包装和重新抛出错误,例如作为自定义业务异常等。