
Spring Boot 프로젝트에서 user_error를 발생시키는 방법은 다음과 같습니다.
1. `UserError` 클래스를 생성하여 custom exception을 정의합니다.
#hostingforum.kr
java
public class UserError extends RuntimeException {
public UserError(String message) {
super(message);
}
}
2. `UserController` 클래스에서 `@ExceptionHandler` 애노테이션을 사용하여 `UserError` 예외를 처리합니다.
#hostingforum.kr
java
@RestController
public class UserController {
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) {
if (id == null) {
throw new UserError("ID는 null일 수 없습니다.");
}
// ...
}
@ExceptionHandler(UserError.class)
public ResponseEntity handleUserError(UserError e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
3. 위 코드에서 `id`가 `null`인 경우 `UserError` 예외가 발생하며, `handleUserError` 메서드에서 처리됩니다.
위 코드에서 `user_error`는 `id`가 `null`인 경우 발생합니다.
2025-03-15 12:55