Search code examples
jacksonhibernate-validatorspring-validatorjavax.validation

Hibernate Validator doesn't validate method parameters annotated with constraint annotations


I have a pojo, which is common for multiple services and each of them has different validation rules for that object. So, I am extending that pojo in each of my services and override some of the setters and throw constraint validations on those overridden methods. The pojo is being constructed from the submitted json over REST call. Jackson is the lib that supposedly should invoke the setter.

    @JsonSetter("name")
    public void setName(@Length(min = 1, max = 50) @NotBlank String name) {
        this.name = name;
    }

Here is my REST method declaration:

public ResponseEntity<?> createEntity(@Valid @RequestBody EntityDTO entity) {

It seems that validation doesn't work in particular with Jackson as I see validateParameters method of org.hibernate.validator.internal.engine.ValidatorImpl invoked on the other methods.

This is how my ValidationConfiguration bean like:

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.validation.MessageInterpolatorFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;

import javax.validation.Validator;

@Configuration
public class ValidationConfiguration {
    public ValidationConfiguration() {
    }

    @Bean(name = "overriddenValidator")
    public Validator validator() {
        LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
        factoryBean.setParameterNameDiscoverer(new CustomParameterNameDiscoverer());
        MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
        factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
        return factoryBean;
    }


    @Bean
    public static MethodValidationPostProcessor methodValidationPostProcessor(Environment environment, @Qualifier("overriddenValidator") Validator validator) {
        validator.forExecutables();
        MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
        boolean proxyTargetClass = (Boolean) environment.getProperty("spring.aop.proxy-target-class", Boolean.class, true);
        processor.setProxyTargetClass(proxyTargetClass);
        processor.setValidator(validator);
        return processor;
    }
}

I also do some validation of request parameters and it works well. However, it doesn't work for this setter.


Solution

  • I couldn't make it work on setters, but, weirdly enough, validations kick in on getters. Even though getters are not invoked explicitly. Seems it has to do with Jackson??

        @Override
        @Length(min = 1, max = 50)
        @NotBlank
        public String getName() {
            return super.getName();
        }