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
@Validand@NotNullannotations before@PathVariablein the controllerUsing primitive type (
longinstead ofLong)Adding
@Validatedannotation to Controller as class level besides using@Validand@NotNullannotations 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"" }
/categories/(then just define a 40x (x as you like) response on@GetMapping("/categories"))... if it was/categegories/null/...what would you excpect?)