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

1 Answer 1

2

Make sure you have

hibernate-validator

dependency and add following bean:

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
}
Sign up to request clarification or add additional context in comments.

3 Comments

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 you could simply create a config class annotated with @Configuration annotation and put it there.
it's not working, I've tried but the behavior was the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.