Search code examples
javaspringspring-bootvalidationspring-validator

Validate path variable size


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?


Solution

  • You can define regex for your path variable

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