In my Spring Boot project when using the following validation rules in my DTO
@Length(max = 70)
@UnwrapValidatedValue
private Optional<String> vendorName;
I am getting the following exception
javax.validation.ValidationException: HV000182: No validation value unwrapper is registered for type java.util.Optional<java.lang.String>
I can't just find anywhere how to register custom validation value unwrapper in Spring
You should register a ValidatedValueUnwrapper
to the validator.
The validator could be configured via the LocalValidatorFactoryBean
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationPropertyMap">
<util:map>
<entry key="hibernate.validator.validated_value_handlers"
value="com.mycompany.validator.OptionalValidatedValueUnwrapper"/>
</util:map>
</property>
</bean>
</beans>
or in java:
@Bean
public LocalValidatorFactoryBean validator() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.getValidationPropertyMap().put("hibernate.validator.validated_value_handlers",
"com.mycompany.validator.OptionalValidatedValueUnwrapper");
return validator;
}
You could find a proper ValidatedValueUnwrapper
implementation in the dropwizard framework (based on the Optional
from guava).
public class OptionalValidatedValueUnwrapper extends ValidatedValueUnwrapper<Optional<?>> {
private final TypeResolver resolver = new TypeResolver();
@Override
public Object handleValidatedValue(final Optional<?> optional) {
return optional.orNull();
}
@Override
public Type getValidatedValueType(final Type type) {
ResolvedType resolvedType = resolver.resolve(type);
return resolvedType.typeParametersFor(Optional.class).get(0).getErasedType();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With