Closed bashnesnos closed 6 years ago
MethodValidationPostProcessor
is ordered with lowest precedence. Your custom post-processor is unordered so it defaults to lowest precedence. If you want to ensure that it is called before the method validation you can do so by giving it an order with higher precedence, for example:
diff --git a/src/main/java/issues/validation/CustomBeanValidationPostProcessor.java b/src/main/java/issues/validation/CustomBeanValidationPostProcessor.java
index 81daa6a..79dc576 100644
--- a/src/main/java/issues/validation/CustomBeanValidationPostProcessor.java
+++ b/src/main/java/issues/validation/CustomBeanValidationPostProcessor.java
@@ -1,5 +1,6 @@
package issues.validation;
+import org.springframework.core.Ordered;
import org.springframework.validation.beanvalidation.BeanValidationPostProcessor;
import java.lang.annotation.ElementType;
@@ -15,7 +16,7 @@ import javax.validation.Validator;
*
* Taken from CAS as is https://github.com/apereo/cas/blob/master/core/cas-server-core-util-api/src/main/java/org/apereo/cas/util/spring/CustomBeanValidationPostProcessor.java
*/
-public class CustomBeanValidationPostProcessor extends BeanValidationPostProcessor {
+public class CustomBeanValidationPostProcessor extends BeanValidationPostProcessor implements Ordered {
/**
* Instantiates a new custom bean validation post processor.
@@ -44,4 +45,9 @@ public class CustomBeanValidationPostProcessor extends BeanValidationPostProcess
setValidator(validator);
}
+
+ @Override
+ public int getOrder() {
+ return 0;
+ }
}
I'm trying to add Spring Cloud Consul into an existing Spring Boot app with spring validation enabled. It's all good until I run the app, and it fails on the validation of a properties bean annotated with
@Validated
As far as I got it, here's what happens:
MethodValidationPostProcessor
creates a proxy for the properties beanCustomBeanValidationPostProcessor
kicks in and tries to validate the proxy fields and fails with the following:Here is a minimum project reproducing the issue: https://github.com/bashnesnos/cas-spring-cloud-consul-issue
Questions:
MethodValidationPostProcessor
for a particular bean?MethodValidationPostProcessor
be ordered after any properties validator? orMethodValidationPostProcessor
check if a bean is even eligible for any method validation? In current example, heartbeatProperties doesn't have any method-scoped validation at all.