2

I found this example how to validate path variable: https://www.mkyong.com/spring-boot/spring-rest-validation-example/

@PostMapping(value = "/payment/{unique_transaction_id}")
      public ResponseEntity<StringResponseDTO> handleWpfMessage(@PathVariable("unique_transaction_id") @Valid @Max(32) String unique_transaction_id) throws Exception {

Can I add @Valid infant of the limitation for String @Max(32) or I have to add @Validated at class level as it is shown in the tutorial?

1 Answer 1

4

You can define regex for your path variable

@PostMapping(value = "/payment/{unique_transaction_id:[a-zA-Z0-9]{0,32}}")

Sign up to request clarification or add additional context in comments.

7 Comments

But should I add @Valid at class level?
No, not needed, @Valid validates all the constraints defined in your request body POJO class.
Is there other way to apply [a-zA-Z0-9] restriction with annotations?
I don't think (or I don't know) there exists any annotation for the same purpose. There are other ways like verifying it in a filter / interceptor but it's not straight forward like above solution.
This might be an enhancement in spring for the same purpose adding a field regex in @PathVariable annotation.
|

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.