Browse Source

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.
pull/1111/head
Rossen Stoyanchev 10 years ago
parent
commit
2292e46b04
  1. 16
      spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandler.java

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 @@ -126,23 +126,25 @@ public class ResponseBodyResultHandler implements HandlerResultHandler, Ordered
@SuppressWarnings("unchecked")
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
Optional<Object> 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<Object> 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<Void>)publisher);
}
}
else {
publisher = Mono.just(value.get());
publisher = Mono.justOrEmpty(result.getReturnValue());
elementType = returnType;
}

Loading…
Cancel
Save