diff --git a/buildSrc/src/main/java/org/springframework/boot/build/context/properties/DocumentConfigurationProperties.java b/buildSrc/src/main/java/org/springframework/boot/build/context/properties/DocumentConfigurationProperties.java index a7ac94f60ced..164350f4088a 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/context/properties/DocumentConfigurationProperties.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/context/properties/DocumentConfigurationProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 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. @@ -101,6 +101,7 @@ private void corePrefixes(Config config) { config.accept("spring.ssl"); config.accept("spring.task"); config.accept("spring.threads"); + config.accept("spring.validation"); config.accept("spring.mandatory-file-encoding"); config.accept("info"); config.accept("spring.output.ansi.enabled"); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.java index a3df37fb1188..2e1571bc288f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 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. @@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnResource; import org.springframework.boot.autoconfigure.condition.SearchStrategy; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.validation.MessageInterpolatorFactory; import org.springframework.boot.validation.beanvalidation.FilteredMethodValidationPostProcessor; import org.springframework.boot.validation.beanvalidation.MethodValidationExcludeFilter; @@ -44,11 +45,13 @@ * * @author Stephane Nicoll * @author Madhura Bhave + * @author Yanming Zhou * @since 1.5.0 */ @AutoConfiguration @ConditionalOnClass(ExecutableValidator.class) @ConditionalOnResource(resources = "classpath:META-INF/services/jakarta.validation.spi.ValidationProvider") +@EnableConfigurationProperties(ValidationProperties.class) @Import(PrimaryDefaultValidatorPostProcessor.class) public class ValidationAutoConfiguration { @@ -68,11 +71,14 @@ public static LocalValidatorFactoryBean defaultValidator(ApplicationContext appl @Bean @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) public static MethodValidationPostProcessor methodValidationPostProcessor(Environment environment, - ObjectProvider validator, ObjectProvider excludeFilters) { + ObjectProvider validator, ObjectProvider excludeFilters, + ObjectProvider validationProperties) { FilteredMethodValidationPostProcessor processor = new FilteredMethodValidationPostProcessor( excludeFilters.orderedStream()); boolean proxyTargetClass = environment.getProperty("spring.aop.proxy-target-class", Boolean.class, true); processor.setProxyTargetClass(proxyTargetClass); + processor + .setAdaptConstraintViolations(validationProperties.getObject().getMethod().isAdaptConstraintViolations()); processor.setValidatorProvider(validator); return processor; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationProperties.java new file mode 100644 index 000000000000..693ed613c2bc --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidationProperties.java @@ -0,0 +1,60 @@ +/* + * Copyright 2012-2025 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.validation; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for validation. + * + * @author Yanming Zhou + * @since 3.5.0 + */ +@ConfigurationProperties(prefix = "spring.validation") +public class ValidationProperties { + + private Method method = new Method(); + + public Method getMethod() { + return this.method; + } + + public void setMethod(Method method) { + this.method = method; + } + + /** + * Method validation properties. + */ + public static class Method { + + /** + * Whether to adapt ConstraintViolations to MethodValidationResult. + */ + private boolean adaptConstraintViolations; + + public boolean isAdaptConstraintViolations() { + return this.adaptConstraintViolations; + } + + public void setAdaptConstraintViolations(boolean adaptConstraintViolations) { + this.adaptConstraintViolations = adaptConstraintViolations; + } + + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationTests.java index 66dc324099c3..2136c1d991fc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 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. @@ -46,6 +46,7 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean; +import org.springframework.validation.method.MethodValidationException; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -59,6 +60,7 @@ * * @author Stephane Nicoll * @author Phillip Webb + * @author Yanming Zhou */ class ValidationAutoConfigurationTests { @@ -207,6 +209,18 @@ void validationCanBeConfiguredToUseJdkProxy() { }); } + @Test + void validationCanBeConfiguredToAdaptConstraintViolations() { + this.contextRunner.withUserConfiguration(AnotherSampleServiceConfiguration.class) + .withPropertyValues("spring.validation.method.adapt-constraint-violations=true") + .run((context) -> { + assertThat(context.getBeansOfType(Validator.class)).hasSize(1); + AnotherSampleService service = context.getBean(AnotherSampleService.class); + service.doSomething(42); + assertThatExceptionOfType(MethodValidationException.class).isThrownBy(() -> service.doSomething(2)); + }); + } + @Test @SuppressWarnings("unchecked") void userDefinedMethodValidationPostProcessorTakesPrecedence() {