From 2292e46b04c65d4f3f8193b0eab249a8058e59a8 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 25 May 2016 17:35:58 -0400 Subject: [PATCH] Update empty return value ResponseBody handling When a null is returned from an @ResponseBody method, rather than returning Mono.empty() immediately, convert it to Mono.empty() and apply the same processing. Currently that doesn't make a practical difference but it's more accurate to do it this way. Eventually it may mean the possibility to turn empty values into something through an extension point as we do with ResponseBodyAdvice in Spring MVC today. --- .../annotation/ResponseBodyResultHandler.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandler.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandler.java index cea0cfc029..fd4ce3c8e3 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandler.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandler.java @@ -126,23 +126,25 @@ public class ResponseBodyResultHandler implements HandlerResultHandler, Ordered @SuppressWarnings("unchecked") public Mono handleResult(ServerWebExchange exchange, HandlerResult result) { - Optional value = result.getReturnValue(); - if (!value.isPresent()) { - return Mono.empty(); - } - Publisher publisher; ResolvableType elementType; ResolvableType returnType = result.getReturnValueType(); + if (this.conversionService.canConvert(returnType.getRawClass(), Publisher.class)) { - publisher = this.conversionService.convert(value.get(), Publisher.class); + Optional optionalValue = result.getReturnValue(); + if (optionalValue.isPresent()) { + publisher = this.conversionService.convert(optionalValue.get(), Publisher.class); + } + else { + publisher = Mono.empty(); + } elementType = returnType.getGeneric(0); if (Void.class.equals(elementType.getRawClass())) { return Mono.from((Publisher)publisher); } } else { - publisher = Mono.just(value.get()); + publisher = Mono.justOrEmpty(result.getReturnValue()); elementType = returnType; } -- GitLab