springspring-mvchibernate-validatorjavax.validation

javax regex validation of path variables in Spring


I have validation working for the beans and request parameters, however, my path variables fail to get validated:

@PathVariable @Pattern(regexp = "[A-Za-z0-9]+") String protocol

When I provide path var as ab!ab it doesn't fail the request with 400 status code but lets it pass with the value assigned to the argument.

I have also validated my regex online and it is valid and should be working fine.

Also, my rest controller does have @Validated annotation.

What am I missing here?

================UPDATE=============

I have tried other constraint annotations and none of them work, so it must something to do with the path variable validation. But what??


Solution

  • Make sure you have

    hibernate-validator

    dependency and add following bean:

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