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.
master
Rossen Stoyanchev 8 years ago
parent 91d063899b
commit 2292e46b04
  1. 16
      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") @SuppressWarnings("unchecked")
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) { public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
Optional<Object> value = result.getReturnValue();
if (!value.isPresent()) {
return Mono.empty();
}
Publisher<?> publisher; Publisher<?> publisher;
ResolvableType elementType; ResolvableType elementType;
ResolvableType returnType = result.getReturnValueType(); ResolvableType returnType = result.getReturnValueType();
if (this.conversionService.canConvert(returnType.getRawClass(), Publisher.class)) { if (this.conversionService.canConvert(returnType.getRawClass(), Publisher.class)) {
publisher = this.conversionService.convert(value.get(), Publisher.class); Optional<Object> optionalValue = result.getReturnValue();
if (optionalValue.isPresent()) {
publisher = this.conversionService.convert(optionalValue.get(), Publisher.class);
}
else {
publisher = Mono.empty();
}
elementType = returnType.getGeneric(0); elementType = returnType.getGeneric(0);
if (Void.class.equals(elementType.getRawClass())) { if (Void.class.equals(elementType.getRawClass())) {
return Mono.from((Publisher<Void>)publisher); return Mono.from((Publisher<Void>)publisher);
} }
} }
else { else {
publisher = Mono.just(value.get()); publisher = Mono.justOrEmpty(result.getReturnValue());
elementType = returnType; elementType = returnType;
} }

Loading…
Cancel
Save