diff --git a/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java b/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java index 1ed1a131bf38..4837675beb44 100644 --- a/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java +++ b/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,6 +56,7 @@ * @author Rossen Stoyanchev * @author Juergen Hoeller * @author Sebastien Deleuze + * @author Yanming Zhou * @since 3.1 */ public class InvocableHandlerMethod extends HandlerMethod { @@ -78,6 +79,9 @@ public class InvocableHandlerMethod extends HandlerMethod { @Nullable private MethodValidator methodValidator; + @Nullable + private Class[] validationGroups; + /** * Create an instance from a {@code HandlerMethod}. @@ -180,7 +184,10 @@ public Object invokeForRequest(NativeWebRequest request, @Nullable ModelAndViewC logger.trace("Arguments: " + Arrays.toString(args)); } - Class[] groups = getValidationGroups(); + Class[] groups = this.validationGroups; + if (groups == null) { + groups = this.validationGroups = this.getValidationGroups(); + } if (shouldValidateArguments() && this.methodValidator != null) { this.methodValidator.applyArgumentValidation( getBean(), getBridgedMethod(), getMethodParameters(), args, groups); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java index 5df721559f1f..3880a0c51d65 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,6 +64,7 @@ * @author Rossen Stoyanchev * @author Juergen Hoeller * @author Sebastien Deleuze + * @author Yanming Zhou * @since 5.0 */ public class InvocableHandlerMethod extends HandlerMethod { @@ -87,6 +88,9 @@ public class InvocableHandlerMethod extends HandlerMethod { @Nullable private MethodValidator methodValidator; + @Nullable + private Class[] validationGroups; + /** * Create an instance from a {@code HandlerMethod}. @@ -166,7 +170,10 @@ public Mono invoke( ServerWebExchange exchange, BindingContext bindingContext, Object... providedArgs) { return getMethodArgumentValues(exchange, bindingContext, providedArgs).flatMap(args -> { - Class[] groups = getValidationGroups(); + Class[] groups = this.validationGroups; + if (groups == null) { + groups = this.validationGroups = this.getValidationGroups(); + } if (shouldValidateArguments() && this.methodValidator != null) { this.methodValidator.applyArgumentValidation( getBean(), getBridgedMethod(), getMethodParameters(), args, groups);