diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java index 4dfe13ee718..31942bccd59 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java @@ -21,6 +21,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.util.List; import java.util.Map; +import java.util.Optional; import reactor.core.publisher.Mono; import reactor.core.publisher.MonoProcessor; @@ -148,11 +149,10 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR } private String getAttributeName(MethodParameter parameter) { - ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class); - if (ann != null && StringUtils.hasText(ann.value())) { - return ann.value(); - } - return Conventions.getVariableNameForParameter(parameter); + return Optional.ofNullable(parameter.getParameterAnnotation(ModelAttribute.class)) + .filter(ann -> StringUtils.hasText(ann.value())) + .map(ModelAttribute::value) + .orElse(Conventions.getVariableNameForParameter(parameter)); } private Mono prepareAttributeMono(String attributeName, ResolvableType attributeType, @@ -216,7 +216,7 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR } // A single data class constructor -> resolve constructor arguments from request parameters. - return exchange.getRequestParams().flatMap(requestParams -> { + return exchange.getRequestParams().map(requestParams -> { ConstructorProperties cp = ctor.getAnnotation(ConstructorProperties.class); String[] paramNames = (cp != null ? cp.value() : parameterNameDiscoverer.getParameterNames(ctor)); Assert.state(paramNames != null, () -> "Cannot resolve parameter names for constructor " + ctor); @@ -234,7 +234,7 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR } args[i] = binder.convertIfNecessary(paramValue, paramTypes[i], new MethodParameter(ctor, i)); } - return Mono.fromSupplier(() -> BeanUtils.instantiateClass(ctor, args)); + return BeanUtils.instantiateClass(ctor, args); }); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelInitializer.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelInitializer.java index 2940e892359..f59f688daf3 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelInitializer.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelInitializer.java @@ -15,10 +15,10 @@ */ package org.springframework.web.reactive.result.method.annotation; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import reactor.core.publisher.Mono; @@ -114,13 +114,12 @@ class ModelInitializer { .orElse(Mono.empty()); } - private String getAttributeName(MethodParameter parameter) { - Method method = parameter.getMethod(); - ModelAttribute annot = AnnotatedElementUtils.findMergedAnnotation(method, ModelAttribute.class); - if (annot != null && StringUtils.hasText(annot.value())) { - return annot.value(); - } - return Conventions.getVariableNameForParameter(parameter); + private String getAttributeName(MethodParameter param) { + return Optional + .ofNullable(AnnotatedElementUtils.findMergedAnnotation(param.getMethod(), ModelAttribute.class)) + .filter(ann -> StringUtils.hasText(ann.value())) + .map(ModelAttribute::value) + .orElse(Conventions.getVariableNameForParameter(param)); } } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java index 5bcf52a2447..85ec9b98714 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java @@ -22,6 +22,7 @@ import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; import reactor.core.publisher.Flux; @@ -276,11 +277,10 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport } private String getNameForReturnValue(MethodParameter returnType) { - ModelAttribute annotation = returnType.getMethodAnnotation(ModelAttribute.class); - if (annotation != null && StringUtils.hasText(annotation.value())) { - return annotation.value(); - } - return Conventions.getVariableNameForParameter(returnType); + return Optional.ofNullable(returnType.getMethodAnnotation(ModelAttribute.class)) + .filter(ann -> StringUtils.hasText(ann.value())) + .map(ModelAttribute::value) + .orElse(Conventions.getVariableNameForParameter(returnType)); } private void updateBindingContext(BindingContext context, ServerWebExchange exchange) {