wuyouzhuguli/SpringAll · error · UserNotExistException
user not exist
Error message
user not exist
What it means
UserNotExistException is a project-defined RuntimeException (cc.mrbird.exception.UserNotExistException) whose super message is hardcoded to "user not exist" and which carries the offending id separately. UserController.get() throws it UNCONDITIONALLY for every GET /user/{id} where id matches \\d+. This is demo code for Spring MVC's @ControllerAdvice / @ExceptionHandler global exception handling, not real lookup logic.
Source
Thrown at 25.Spring-Boot-Exception/src/main/java/cc/mrbird/controller/UserController.java:15
package cc.mrbird.controller;
import cc.mrbird.exception.UserNotExistException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
public class UserController {
@GetMapping("/{id:\\d+}")
public void get(@PathVariable String id) {
throw new UserNotExistException(id);
}
}
View on GitHub (pinned to 614d2578d9)
Solutions
- Register a @ControllerAdvice with @ExceptionHandler(UserNotExistException.class) returning a structured error body and HTTP status (e.g., 400 or 404).
- Replace the unconditional throw with a real lookup (userService.findById(id)) and throw only when the result is null.
- If this is throwaway demo code, remove the endpoint before shipping.
- Verify the exception's message/id fields are what your handler serializes to the client.
Example fix
// before
@GetMapping("/{id:\\d+}")
public void get(@PathVariable String id) {
throw new UserNotExistException(id);
}
// after
@GetMapping("/{id:\\d+}")
public User get(@PathVariable String id) {
User user = userService.findById(id);
if (user == null) {
throw new UserNotExistException(id);
}
return user;
} Defensive patterns
Strategy: try-catch
Try / catch
// Register once via @ControllerAdvice
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(UserNotExistException.class)
public ResponseEntity<Map<String, Object>> handle(UserNotExistException e) {
Map<String, Object> body = new HashMap<>();
body.put("id", e.getId());
body.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(body);
}
} Prevention
- Always pair a custom RuntimeException with a global @ExceptionHandler so it serializes to a clean response instead of a 500.
- Do not throw unconditionally in a controller; gate the throw behind a real null check.
- Strip demo endpoints before shipping to production.
When it happens
Trigger: Any HTTP GET to /user/1, /user/42, etc. (path variable constrained to digits). The method always throws regardless of whether such a user exists.
Common situations: Developer left the demo throw in place expecting real service code; an @ExceptionHandler(UserNotExistException.class) is missing so Spring returns a default 500; the unconditional throw masks a missing findById call; tests hit the endpoint and see the literal message.
Related errors
AI-assisted analysis of wuyouzhuguli/SpringAll@614d2578d9 (2026-08-14).
Data as JSON: /api/errors/c610a6623bfc3a29.
Report an issue: GitHub.