0

In Spring Boot applications, I have problem with validating @PathVariable for long type and even if I tried the following approaches, I cannot validate @PathVariable for null values (whereas I can using @RequestParam):

  • Trying to use @Valid and @NotNull annotations before @PathVariable in the controller

  • Using primitive type (long instead of Long)

  • Adding @Validated annotation to Controller as class level besides using @Valid and @NotNull annotations in the Controller method.

So, here is my Controller method and I am wondering, if it is not possible to validate null path variable? Actually Swagger does not let the user send request with null path variable, but Postman lets.

@GetMapping("/categories/{id}")
public ResponseEntity<ApiResponse<CategoryDto>> findById(@PathVariable long id) {
    // ...
}

Here is the error message after sending null path variable on Postman:

"message": "Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException: For input string: ":id"" }

5
  • 1
    Does this answer your question? How to validate Spring MVC @PathVariable values? Commented Dec 3, 2022 at 18:44
  • how did you send "null path variable" on postman? (I hope /categories/ (then just define a 40x (x as you like) response on @GetMapping("/categories"))... if it was /categegories/null/...what would you excpect?) Commented Dec 3, 2022 at 18:47
  • path variables should never be nullable. On the other hand I can recommend: to use "nullable datatypes" on "nonnullable fields" ... Commented Dec 3, 2022 at 18:52
  • @Alien Actually I read that page, but I am not sure if you look at the dates of the answers. I think during these 6-7 years, something must be solved. That's why I needed to create a new question. Commented Dec 3, 2022 at 20:26
  • @xerox Just make id fields empty and then try to send null id. Commented Dec 3, 2022 at 20:26

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.