๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
2026-03-01 12:20:10.800 WARN 8842 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' from value 'abc' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "abc"]โก Quick Fix Works 80% of the time
Ensure the client sends a numeric value in the URL, or handle the exception globally.
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) { ... }
// Client must call: /users/123 (NOT /users/abc)๐ง Why this Happens
Tap to expand the deep technical explanation
Your @PathVariable is declared as a numeric type (like Long or Integer), but the actual text in the URL was a non-numeric string (like 'abc'). Spring tried to convert 'abc' to a Long, failed, and threw this exception.
The HITEC City Parking Spot Analogy:
Imagine a parking garage that only accepts cars. A motorcycle tries to enter, but the barrier sensor (Spring) can't convert 'motorcycle' into a 'car' type, so it refuses entry.
๐ How to Reproduce Confirm this is your error
Call a route like /users/abc where the @PathVariable is typed as Long. Spring cannot convert 'abc' and throws TypeMismatchException: Failed to convert value of type String to required type Long.
๐ ๏ธ Solutions (3 Ways to Fix)
Fix the client URL
๐ Use this if the client is sending a string where an integer/long is expected.
If your @PathVariable is of type Long, the URL must contain a valid number. 'abc' cannot be converted to a Long.
// Controller
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) { ... }
// Correct Client Call
GET /users/123
// Wrong Client Call
GET /users/abcHandle the exception globally
๐ Use this to return a clean JSON error instead of Spring's default 400 HTML page.
Create a global exception handler to catch MethodArgumentTypeMismatchException and return a friendly message.
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<String> handleTypeMismatch(MethodArgumentTypeMismatchException ex) {
return ResponseEntity.badRequest().body("Invalid ID format. Must be a number.");
}
}Use String and parse manually
๐ Use this if you want custom validation logic on the ID.
Accept the path variable as a String, then try to parse it to a Long inside your method, handling the exception yourself.
@GetMapping("/users/{id}")
public ResponseEntity<?> getUser(@PathVariable String id) {
try {
Long longId = Long.parseLong(id);
return ResponseEntity.ok(userService.getUser(longId));
} catch (NumberFormatException e) {
return ResponseEntity.badRequest().body("ID must be numeric");
}
}๐ Version Notes
Throws MethodArgumentTypeMismatchException.
Throws MethodArgumentTypeMismatchException, sometimes wrapped in ProblemDetail.
๐ก๏ธ How to Prevent This Next Time
Ensure frontend APIs construct URLs with the correct data types. Always handle TypeMismatchException globally to avoid exposing stack traces.