4

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??

Ihor M.
  • 2,728
  • 3
  • 44
  • 70

1 Answers1

2

Make sure you have

hibernate-validator

dependency and add following bean:

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
}
Vertigo
  • 107
  • 7
  • For anyone else that are also new to this. I inserted the Bean in the class with the @SpringBootApplication annotation (where your main method is declared). If any of you proos out there know of a better place I am all ears. But it worked like a charm! – W_O_L_F Jun 17 '20 at 09:16
  • 1
    @W_O_L_F you could simply create a config class annotated with `@Configuration` annotation and put it there. – Vertigo Nov 05 '20 at 18:50
  • it's not working, I've tried but the behavior was the same. – Loc Truong May 14 '21 at 06:16