0

I cannot validate a string type path variable in Spring Boot app. I tried the suggestions mentioned on How to validate Spring MVC @PathVariable values?, but not working. So, do I have to create a custom validator just for validating a @PathVariable value?

import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;

@RestController
@Validated
@RequestMapping("/api/v1")
public class DataController {


    @GetMapping("/employees/{code}")
    public ResponseEntity<ApiResponse<List<Employee>>> findAllByCode(
            // @PathVariable("code") @Valid @NotBlank String code) {
            @PathVariable("code") @NotBlank String code) {
      // ...
    }
}

Any idea about the solution?

2
  • Does anybody else has never used validation ever before? Commented Nov 25, 2022 at 8:45
  • The option from the linked page works fine for me, it looks like you just need to override org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleBindException in a class, which will extend ResponseEntityExceptionHandle and having annotated as @ControllerAdvice. This is just one of the ways. Commented Feb 24, 2023 at 13:06

0

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.