Hot questions for Spring Validator

Top 10 Java Open Source / Spring / Spring Validator

Question:

I have been trying to add spring validators to a spring-data-rest project.

I followed along and setup the "getting started" application via this link: http://spring.io/guides/gs/accessing-data-rest/

...and now I am trying to add a custom PeopleValidator by following the documents here: http://docs.spring.io/spring-data/rest/docs/2.1.0.RELEASE/reference/html/validation-chapter.html

My custom PeopleValidator looks like

package hello;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class PeopleValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return true;
    }

    @Override
    public void validate(Object target, Errors errors) {
        errors.reject("DIE");
    }
}

...and my Application.java class now looks like this

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;

@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public PeopleValidator beforeCreatePeopleValidator() {
        return new PeopleValidator();
    }
}

I would expect that POSTing to the http://localhost:8080/people URL would result in an error of some kind since the PeopleValidator is rejecting everything. However, no error is thrown, and the validator is never called.

I have also tried manually setting up the validator as shown in section 5.1 of the spring-data-rest documentation.

What am I missing?


Answer:

So it appears that the before/after "save" events only fire on PUT and PATCH. When POSTing, the before/after "create" events fire.

I tried it the manual way again using the configureValidatingRepositoryEventListener override and it worked. I'm not sure what I'm doing differently at work than here at home. I'll have to look tomorrow.

I sure would love to hear if others have suggestions on why it wouldn't work.

For the record, here is what the new Application.java class looks like.

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;

@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
public class Application extends RepositoryRestMvcConfiguration {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
        validatingListener.addValidator("beforeCreate", new PeopleValidator());
    }
}

Question:

I have a class with hibernate's validation annotation on some fields (such as @NotNull and @Size(min = 4, max = 50), etc...)

public class MyClass {

    Long id;

    @NotEmpty
    @Size(min = 4, max = 50)
    String machineName;

    @NotEmpty
    @Size(min = 4, max = 50)
    String humanName;

    // Getters, setters, etc…
}

I also have a custom controller that acts as a JSON API, and a JSON deserializer that creates MyClass objects when API methods are called. In my custom controller I have a method to create a new object of that type:

@RequestMapping(method = RequestMethod.POST)
public long createMyObject(@RequestBody @Valid MyClass newObj) {
    // Create the object in the database
    return newObj.getId();
}

and another method that updates an existing object

@RequestMapping(method = RequestMethod.PUT)
public void updateMyObject(@RequestBody MyClass updatedObj) {
    MyClass existingObj = // Get existing obj from DB by updatedObj.getId();

    // Do some secondary validation, such as making sure that a specific
    // field remains unchanged compared to the existing instance
    if (existingObj.getMachineName() != null && 
            !existingObj.getMachineName().equals(updatedObj.getMachineName())) {
        throw new CannotChangeMachineNameException();
    }
    else {
        updatedObj.setMachineName(existingObj.getMachineName());
    }

    // [HERE IS WHERE I WANT THE MAGIC TO HAPPEN]

    // Save updatedObj to the database
}

While I can use @Valid in createMyObject, I cannot use it in updateMyObject because our API implementation requires that machineName remains unchanged - users can call the API with a JSON object that either excludes machineName entirely or populate it with the same value that exists in the database.*

Before saving the updated object to the database I want to call the same validator that having the @Valid annotation would cause to be called. How can I find this validator and use it?


Answer:

Nothing says you need to use @Valid in your controller methods only. Why not make a validation method that accepts a parameter you annotate as @Valid, then just return that same parameter.

Like this:

public Book validateBook(@Valid Book book) {
   return book;
}

Looks like an alternative would be to use Hibernate's validation package. Here's it's documentation.

Basically, you get a Validator from a ValidationFactory, and then use the validator like this:

 @Test
    public void manufacturerIsNull() {
        Car car = new Car(null, "DD-AB-123", 4);

        Set<ConstraintViolation<Car>> constraintViolations =
            validator.validate(car);

        assertEquals(1, constraintViolations.size());
        assertEquals("may not be null", constraintViolations.iterator().next().getMessage());
}

Question:

I was looking at the @org.hibernate.validator.constaints.NotEmpty annotation:

@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
@Size(min = 1)
public @interface NotEmpty {
    String message() default "{org.hibernate.validator.constraints.NotEmpty.message}";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

    /**
     * Defines several {@code @NotEmpty} annotations on the same element.
     */
    @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
    @Retention(RUNTIME)
    @Documented
    public @interface List {
        NotEmpty[] value();
    }
}

I'm confused by the last part:

    /**
     * Defines several {@code @NotEmpty} annotations on the same element.
     */
    @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
    @Retention(RUNTIME)
    @Documented
    public @interface List {
        NotEmpty[] value();
    }

I'm not sure how that works, nor how to use it. From my understanding, anything under Java 8 does not allow repeated annotations on the same element.

Can anybody clarify?


Answer:

Reason why NotEmpty.List exists, is to go around the fact that same annotation cannot be repeated for same element. With the help of NotEmpty.List multiple NotEmpty annotations are effectively applied to one element. Annotation processing checks through the list of NotEmpty annotations that are the value of NotEmpty.List.

In the case of NotEmpty one reason for using List of validators could be use of groups and assigning different messages per group.

For the sake of example, let's take entity which can represent either company or person. In both cases name should not be null, but messages differ:

@NotEmpty.List({
    @NotEmpty( message = "Person name should not be empty",   
               groups=PersonValidations.class),
    @NotEmpty( message = "Company name should not be empty",    
               groups=CompanyValidations.class),
})
private String name;

Question:

I have a model class which has list of Strings. The list can either be empty or have elements in it. If it has elements, those elements can not be empty. For an example suppose I have a class called QuestionPaper which has a list of questionIds each of which is a string.

class QuestionPaper{
private List<String> questionIds;
....
}

The paper can have zero or more questions. But if it has questions, the id values can not be empty strings. I am writing a micro service using SpringBoot, Hibernate, JPA and Java. How can I do this validation. Any help is appreciated.

For an example we need to reject the following json input from a user.

{ "examId": 1, "questionIds": [ "", " ", "10103" ] }

Is there any out of the box way of achieving this, or will I have to write a custom validator for this.


Answer:

Custom validation annotation shouldn't be a problem:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = NotEmptyFieldsValidator.class)
public @interface NotEmptyFields {

    String message() default "List cannot contain empty fields";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

}


public class NotEmptyFieldsValidator implements ConstraintValidator<NotEmptyFields, List<String>> {

    @Override
    public void initialize(NotEmptyFields notEmptyFields) {
    }

    @Override
    public boolean isValid(List<String> objects, ConstraintValidatorContext context) {
        return objects.stream().allMatch(nef -> nef != null && !nef.trim().isEmpty());
    }

}

Usage? Simple:

class QuestionPaper{

    @NotEmptyFields
    private List<String> questionIds;
    // getters and setters
}

P.S. Didn't test the logic, but I guess it's good.

Question:

I have a java bean being used to send JSON messages to a spring @RestController and I have bean validation setup and running just fine using @Valid. But I want to move to Protobuf/Thrift and move away from REST. It is an internal API and a lot of big companies have done away with REST internally. What this really means is that I no longer have control of the message objects - they are generated externally. I can't put annotations on them anymore.

So now my validation has to be programmatic. How do I do this? I have coded up a Validator and it works just great. But it doesn't use the nice @Valid annotation. I have to do the following:

@Service
public StuffEndpoint implements StuffThriftDef.Iface {

    @Autowired
    private MyValidator myValidator;

    public void things(MyMessage msg) throws BindException {
        BindingResult errors = new BeanPropertyBindingResult(msg, msg.getClass().getName());
        errors = myValidator.validate(msg);
        if (errors.hasErrors()) {
            throw new BindException(errors);
        } else {
            doRealWork();
        }
    }
}

This stinks. I have to do this in every single method. Now, I can put a lot of that into one method that throws BindException and that makes it one line of code to add to every method. But that's still not great.

What I want is to see it look like this:

@Service
@Validated
public StuffEndpoint implements StuffThriftDef.Iface {

    public void things(@Valid MyMessage msg) {
        doRealWork();
    }
}

And still get the same result. Remember, my bean has no annotations. And yes, I know I can use the @InitBinder annotation on a method. But that only works for web requests.

I don't mind injecting the correct Validator into this class, but I would prefer if my ValidatorFactory could pull the correct one based on the supports() method.

Is this possible? Is there a way to configure bean validation to actually use Spring validation instead? Do I have to hijack a Aspect somewhere? Hack into the LocalValidatorFactory or the MethodValidationPostProcessor?

Thanks.


Answer:

Its pretty complicated thing to combine Spring validation and JSR-303 constrains. And there is no 'ready to use' way. The main inconvenience is that Spring validation uses BindingResult, and JSR-303 uses ConstraintValidatorContext as result of validation.

You can try to make your own validation engine, using Spring AOP. Let's consider, what we need to do for it. First of all, declare AOP dependencies (if you didn't yet):

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.2.4.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
   <version>1.8.8</version>
   <scope>runtime</scope>
</dependency>
<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.8.8</version>
</dependency>

I'm using Spring of version 4.2.4.RELEASE, but of cause you can use your own. AspectJ needed for use aspect annotation. Next step, we have to create simple validator registry:

public class CustomValidatorRegistry {

    private List<Validator> validatorList = new ArrayList<>();

    public void addValidator(Validator validator){
        validatorList.add(validator);
    }

    public List<Validator> getValidatorsForObject(Object o) {
        List<Validator> result = new ArrayList<>();
        for(Validator validator : validatorList){
            if(validator.supports(o.getClass())){
                result.add(validator);
            }
        }
        return result;
    }
}

As you see it is very simple class, which allow us to find validator for object. Now lets create annotation, that will be mark methods, that need to be validated:

package com.mydomain.validation;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomValidation {
}

Because of standard BindingException class is not RuntimeException, we can't use it in overriden methods. This means we need define our own exception:

public class CustomValidatorException extends RuntimeException {

    private BindingResult bindingResult;

    public CustomValidatorException(BindingResult bindingResult){
        this.bindingResult = bindingResult;
    }

    public BindingResult getBindingResult() {
        return bindingResult;
    }
}

Now we are ready to create an aspect that will do most of the work. Aspect will execute before methods, which marked with CustomValidation annotation:

@Aspect
@Component
public class CustomValidatingAspect {

    @Autowired
    private CustomValidatorRegistry registry; //aspect will use our validator registry


    @Before(value = "execution(public * *(..)) && annotation(com.mydomain.validation.CustomValidation)")
    public void doBefore(JoinPoint point){
        Annotation[][] paramAnnotations  =
                ((MethodSignature)point.getSignature()).getMethod().getParameterAnnotations();
        for(int i=0; i<paramAnnotations.length; i++){
            for(Annotation annotation : paramAnnotations[i]){
                //checking for standard org.springframework.validation.annotation.Validated
                if(annotation.annotationType() == Validated.class){
                    Object arg = point.getArgs()[i];
                    if(arg==null) continue;
                    validate(arg);
                }
            }
        }
    }

    private void validate(Object arg) {
        List<Validator> validatorList = registry.getValidatorsForObject(arg);
        for(Validator validator : validatorList){
            BindingResult errors = new BeanPropertyBindingResult(arg, arg.getClass().getSimpleName());
            validator.validate(arg, errors);
            if(errors.hasErrors()){
                throw new CustomValidatorException(errors);
            }
        }
    }
}

execution(public * *(..)) && @annotation(com.springapp.mvc.validators.CustomValidation) means, that this aspect will applied to any public methods of beans, which marked with @CustomValidation annotation. Also note, that to mark validated parameters we are using standard org.springframework.validation.annotation.Validated annotation. But of cause we could make our custom. I think other code of aspect is very simple and does not need any comments. Further code of example validator:

public class PersonValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return aClass==Person.class;
    }

    @Override
    public void validate(Object o, Errors errors) {
        Person person = (Person)o;
        if(person.getAge()<=0){
            errors.rejectValue("age", "Age is too small");
        }
    }
}

Now we have make tune the configuration and all ready to use:

@Configuration
@ComponentScan(basePackages = "com.mydomain")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AppConfig{

    .....

    @Bean
    public CustomValidatorRegistry validatorRegistry(){
        CustomValidatorRegistry registry = new CustomValidatorRegistry();
        registry.addValidator(new PersonValidator());
        return registry;
    }    
}

Note, proxyTargetClass is true because we will use cglib class proxy.


Example of target method in service class:

@Service
public class PersonService{

    @CustomValidation
    public void savePerson(@Validated Person person){        
       ....
    }

}

Because of @CustomValidation annotation aspect will be applied, and because of @Validated annotation person will be validated. And example of usage of service in controller(or any other class):

@Controller
public class PersonConroller{

    @Autowired
    private PersonService service;

    public String savePerson(@ModelAttribute Person person, ModelMap model){
        try{
            service.savePerson(person);
        }catch(CustomValidatorException e){
            model.addAttribute("errors", e.getBindingResult());
            return "viewname";
        }
        return "viewname";
    }

}

Keep in mind, that if you will invoke @CustomValidation from methods of PersonService class, validation will not work. Because it will invoke methods of original class, but not proxy. This means, that you can invoke this methods only from outside of class (from other classes), if you want validation to be working (eg @Transactional works same way).

Sorry for long post. My answer is not about 'simple declarative way', and possible you will do not need it. But I was curious resolve this problem.

Question:

Is there a way to validate primitive (int, String etc.) GET parameters using annotations?

@RequestMapping(value = "/api/{someInt}",
    method = RequestMethod.GET,
    produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> someRestApiMethod(
    @PathVariable
    @Valid @Min(0) @Digits(integer=10, fraction=0)
    int someInt) {

    //...

    return new ResponseEntity<String>("sample:"+someInt, HttpStatus.OK);
}

As you see I have put a bunch of annotations to validate someInt to be a positive integer with 10 digits but still it accepts every kind of integers.


Answer:

Yes, this is possible.

Given the following controller:

@RestController
@Validated
public class ValidatingController {

    @RequestMapping("/{id}")
    public int validatedPath(@PathVariable("id") @Max(9) int id) {
        return id;
    }

    @ExceptionHandler
    public String constraintViolationHandler(ConstraintViolationException ex) {
        return ex.getConstraintViolations().iterator().next()
                .getMessage();
    }
}

and a MethodValidationPostProcessor registered in your context as follows (or XML equivalent, and not necessary with the Spring Boot web starter - it will do this for you):

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
}

Assuming your dispatcher servlet is mapped to http://localhost:8080/:

  • accessing http://localhost:8080/9 gives 9
  • accessing http://localhost:8080/10 gives must be less than or equal to 9

It looks like moves are afoot to make this easier/more automatic in future versions of Spring.

Question:

I have a User entity having email property annotated with @Email

@Email
private String email;

I am using @Valid (javax.validation.Valid) annotation on my Controller class. The issue is that the controller validator is passing the invalid emails. Example: pusp@1 - obviously this is an invalid email address pusp@fake The pattern I noticed is, the @Email only want sometext@text, it don't care for the extensions(.com/org etc). Is it the expected behaviour? Do I need to pass my own regex implementation for @Email(regex="")


Answer:

A email without . may be considered as valid according to the validators. In a general way, validator implementations (here it is probably the Hibernate Validator) are not very restrictive about emails. For example the org.hibernate.validator.internal.constraintvalidators.AbstractEmailValidator javadoc states :

The specification of a valid email can be found in RFC 2822 and one can come up with a regular expression matching all valid email addresses as per specification. However, as this article discusses it is not necessarily practical to implement a 100% compliant email validator. This implementation is a trade-off trying to match most email while ignoring for example emails with double quotes or comments.

And as a side note, I noticed similarly things with HTML Validator for emails.

So I think that the behavior that you encounter actually is which one expected. And about your question :

Do I need to pass my own regex implementation for @Email(regex="")

Indeed. You don't have any other choice if you want to make the validation more restrictive. As alternative, this answer creating its own validator via a constraints composition is really interesting as it is DRY (you can reuse your custom ConstraintValidator without specified at each time the pattern as it will be included in) and it reuses the "good part" of the @Email ConstraintValidator :

@Email(message="Please provide a valid email address")
@Pattern(regexp=".+@.+\\..+", message="Please provide a valid email address")
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@Documented
public @interface ExtendedEmailValidator {
    String message() default "Please provide a valid email address";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

Question:

I have some model class

public class Account {

    @Email
    private String email;

    @NotNull
    private String rule;
}

and spring-validator

public class AccountValidator implements Validator {

    @Override
    public boolean supports(Class aClass) {
        return Account.class.equals(aClass);
    }

    @Override
    public void validate(Object obj, Errors errors) {
        Account account = (Account) obj;
        ValidationUtils.rejectIfEmpty(errors, "email", "email.required");
        ValidationUtils.rejectIfEmpty(errors, "rule", "rule.required");

        complexValidateRule(account.getRule(), errors);
    }

    private void complexValidateRule(String rule, Errors errors) {
        // ...
    }
}

I run in my service

AccountValidator validator = new AccountValidator();
Errors errors = new BeanPropertyBindingResult(account, "account");
validator.validate(account, errors);

Can I add to my validation process constraints @Email, @NotNull (JSR-303) and don't describe these rules in AccountValidator?

I know how works @Valid in spring-controllers, but what's about service layer? Is it possible? How to do such kind of validation in a proper way? May I should use Hibernate Validator?


Answer:

Spring provides an Adapter to merge both validation APIs. See the current Spring JavaDoc for more information.

An possible implementation would be

public class AccountValidator implements Validator {

  private final SpringValidatorAdapter validator;

  public AccountValidator(SpringValidatorAdapter validator) {
      super();
      this.validator = validator;
  }

  @Override
  public boolean supports(Class aClass) {
      return Account.class.equals(aClass);
  }

  @Override
  public void validate(Object obj, Errors errors) {

      //jsr303
      validator.validate(obj, errors);

      //custom rules
      Account account = (Account) obj;
      complexValidateRule(account.getRule(), errors);
  }

  private void complexValidateRule(String rule, Errors errors) {
      // ...
  }
}

Question:

I know about @Valid annotation to instruct spring to validate for example a Controller argument according to JSR-303 in such this example:

@GetMapping("/test")
public TestDTO testDTO(@Valid TestDTO testDTO){
        return testDTO;
}

But I would like to be able to configure Spring in some way to enable validation in all my controllers without specify explicitly the @Valid annotation.

Is that possible in any way? Some Spring configuration? Making use of AOP?...


Answer:

I have finally came across with a working solution which may be not the optimal from the point of view of Spring configuration (as I said I'm Spring beginner).

The idea was to modify the argument resolvers (the ones that implement HandlerMethodArgumentResolver), replacing the argument resolver associated to arguments with a @RequestBody annotation. Creating an inherited class from the default one (which is RequestResponseBodyMethodProcessor) and overriding a method in the class hierarchy which efectively determines if perform a validation or not (based in the presence of @Valid, @Validated, @ValidXxxxxx annotations as the default behaviour), making to always validate with no further check.

So here is the code (I'm using Java 8 BTW):

Extend RequestResponseBodyMethodProcessor to define validation strategy (in this case, always validate):

public class MyRequestResponseBodyMethodProcessor extends RequestResponseBodyMethodProcessor {

    public MyRequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters) {
        super(converters);
    }

    @Override
    protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
        binder.validate();      // always validating @RequestMapping annotated parameters ;)
    }
}

Define a @Configuration class where to replace default argument resolver:

@Configuration
public class MyValidationAdapterConfigurer {

    @Autowired
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    // Injecting your own resolver
    @Autowired
    private RequestResponseBodyMethodProcessor requestResponseBodyMethodProcessor;


    @PostConstruct
    public void init() {

        // Don't know why but, removing the target resolver and adding the injected one to the end does not work!
        // Must be something related with the resolvers ordering. So just replacing the target in the same position.
        final List<HandlerMethodArgumentResolver> mangledResolvers = requestMappingHandlerAdapter.getArgumentResolvers().stream()
            .map(resolver -> resolver.getClass().equals(RequestResponseBodyMethodProcessor.class) ?
                requestResponseBodyMethodProcessor: resolver)
            .collect(Collectors.toList());

        requestMappingHandlerAdapter.setArgumentResolvers(mangledResolvers);
    }

}

Finally configure Spring to deliver your customized Bean in your Application configuration class:

@Configuration
@PropertySource("classpath:api.properties")
public class MyRestApiConfiguration {

    @Bean
    @Autowired
    RequestResponseBodyMethodProcessor requestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters) {
        return new MyRequestResponseBodyMethodProcessor(converters);
    }

}

Question:

I am implementing custom validation annotation using JSR 303 and I am getting below error. I am following details given at Cross field validation with Hibernate Validator (JSR 303)

java.lang.ClassCastException: com.sun.proxy.$Proxy95 cannot be cast to com.my.validator.FieldMatch
at com.my.validator.FieldMatchValidator.initialize(FieldMatchValidator.java:14) ~[classes/:na]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.initializeConstraint(ConstraintValidatorManager.java:261) ~[hibernate-validator-5.2.4.Final.jar:5.2.4.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.createAndInitializeValidator(ConstraintValidatorManager.java:183) ~[hibernate-validator-5.2.4.Final.jar:5.2.4.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.getInitializedValidator(ConstraintValidatorManager.java:122) ~[hibernate-validator-5.2.4.Final.jar:5.2.4.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:303) ~[hibernate-validator-5.2.4.Final.jar:5.2.4.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(ConstraintTree.java:244) ~[hibernate-validator-5.2.4.Final.jar:5.2.4.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:163) ~[hibernate-validator-5.2.4.Final.jar:5.2.4.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:116) ~[hibernate-validator-5.2.4.Final.jar:5.2.4.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:87) ~[hibernate-validator-5.2.4.Final.jar:5.2.4.Final]
at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:73) ~[hibernate-validator-5.2.4.Final.jar:5.2.4.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:617) ~[hibernate-validator-5.2.4.Final.jar:5.2.4.Final]

Can someone please help me?


Answer:

If you have written custom validation annotation then please make sure @Constraint(validatedBy = FieldMatchValidator.class) has correct validator class. If validatedBy has different class then you will get ClassCastException. Please refer the similar issue reported at How to solve cast issue java.lang.ClassCastException: $Proxy cannot be cast to NotEmpty wich is annotation

@Target({TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch
{
}

Question:

I am unit testing a Spring controllers post method (using org.springframework.test.web.servlet.MockMvc), and I'm trying to confirm that when there are validation errors in the form it will send the view back to the form by checking the BindingResult.hasErrors method.

Here is my test

  @Test
  public void testFilterChannelProgrammesWhenChannelListAndGenreListAreEmptyAndProgNameIsTooLong() throws Exception {
    String progName = TestUtil.createStringWithLength(301);

    mockMvc.perform(post("/api/filter")
        .contentType(MediaType.APPLICATION_FORM_URLENCODED)
        .param("progName", progName)
        .sessionAttr("filter", new ProgrammeSearchDTO())
        )
        .andExpect(status().isOk())
        .andExpect(view().name("api/filter"))
        .andExpect(forwardedUrl("/WEB-INF/jsp/api/filter.jsp"))
        .andExpect(model().attributeHasFieldErrors("filter", "progName"))
        .andExpect(model().attributeHasFieldErrors("filter", "genreIdList"))
        .andExpect(model().attributeHasFieldErrors("filter", "channelIdList"))        
        .andExpect(model().attribute("filter", hasProperty("progName", is(progName))));

    verifyZeroInteractions(channelProgrammeServiceMock);
  }

Here is the DTO that the session attribute is bound to

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

public class ProgrammeSearchDTO {

  @NotEmpty
  private String[] channelIdList;

  @NotEmpty
  private String[] genreIdList;

  private String fromDateTime;
  private String toDateTime;

  @Length(max = 200)
  private String progName;

  private boolean subtitled;
  private boolean signed;
  private boolean film;

  public String[] getChannelIdList() {
    return channelIdList;
  }

  public String getFromDateTime() {
    return fromDateTime;
  }

  public String[] getGenreIdList() {
    return genreIdList;
  }  

  public String getProgName() {
    return progName;
  }

  public String getToDateTime() {
    return toDateTime;
  }

  public boolean isFilm() {
    return film;
  }

  public boolean isSigned() {
    return signed;
  }

  public boolean isSubtitled() {
    return subtitled;
  }

  public void setChannelIdList(String[] channelIdList) {
    this.channelIdList = channelIdList;
  }

  public void setFilm(boolean film) {
    this.film = film;
  }

  public void setFromDateTime(String fromDateTime) {
    this.fromDateTime = fromDateTime;
  }

  public void setGenreIdList(String[] genreIdList) {
    this.genreIdList = genreIdList;
  }

  public void setProgName(String progName) {
    this.progName = progName;
  }

  public void setSigned(boolean signed) {
    this.signed = signed;
  }

  public void setSubtitled(boolean subtitled) {
    this.subtitled = subtitled;
  }

  public void setToDateTime(String toDateTime) {
    this.toDateTime = toDateTime;
  }  


}

And the controller method

  @RequestMapping(value = "/api/filter", method = RequestMethod.POST)
  public String filterChannelProgrammes(@Valid @ModelAttribute ProgrammeSearchDTO programmeSearchDTO, BindingResult result, Model model) {
    if(result.hasErrors()) {
      return "api/filter";
    }
    model.addAttribute("results", null);
    return "redirect:filterResults";
  }

For this test the return "api/filter"; should be actioned, but hasErrors() is always false. I have also tried with the following

  @RequestMapping(value = "/api/filter", method = RequestMethod.POST)
  public String filterChannelProgrammes(@Valid @ModelAttribute("filter") ProgrammeSearchDTO programmeSearchDTO, BindingResult result, Model model) {
    if(result.hasErrors()) {
      return "api/filter";
    }
    model.addAttribute("results", null);
    return "redirect:filterResults";
  }

But hasErrors() is still false

EDIT

After some more digging I have this sorted, it also required the following in the context config xml

<mvc:annotation-driven />

<bean id="validator"
      class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

And these dependencies in the maven pom.xml

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.1.2.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>2.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.el</artifactId>
        <version>2.2.4</version>
    </dependency>

Answer:

After some more digging I have this sorted, it also required the following in the context config xml

<mvc:annotation-driven />

<bean id="validator"
      class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

And these dependencies in the maven pom.xml

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.1.2.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>2.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.el</artifactId>
        <version>2.2.4</version>
    </dependency>