Loading...
Spring Framework Reference Documentation 7.0.2의 Exceptions의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
See equivalent in the Servlet stack
@Controller 및 @ControllerAdvice class는
controller method에서 발생한 exception을 처리하기 위한 @ExceptionHandler method를 가질 수 있습니다. 다음
예제는 그러한 handler method를 포함합니다:
1import java.io.IOException; 2 3import org.springframework.http.ResponseEntity; 4import org.springframework.stereotype.Controller; 5import org.springframework.web.bind.annotation.ExceptionHandler; 6 7@Controller 8public class SimpleController { 9 10 @ExceptionHandler(IOException.class) 11 public ResponseEntity<String> handle() { 12 return ResponseEntity.internalServerError().body("Could not read file storage"); 13 } 14 15}
1import org.springframework.http.ResponseEntity 2import org.springframework.stereotype.Controller 3import org.springframework.web.bind.annotation.ExceptionHandler 4import java.io.IOException 5 6@Controller 7class SimpleController { 8 9 @ExceptionHandler(IOException::class) 10 fun handle() : ResponseEntity<String> { 11 return ResponseEntity.internalServerError().body("Could not read file storage") 12 } 13 14}
exception은 전파되는 top-level exception(즉, 직접적인 IOException이 throw되는 경우)이나 top-level wrapper
exception 내의 immediate cause(예를 들어, IllegalStateException 안에 wrapping된 IOException)에 대해
매칭될 수 있습니다.
exception type을 매칭하기 위해, 선호되는 방식은 target exception을 앞의 예제에서 보여준 것처럼 method argument로 선언하는 것입니다. 대안으로, annotation 선언이 매칭할 exception type을 좁힐 수 있습니다.
일반적으로 argument signature에서 가능한 한 구체적으로 작성하고, 해당하는 order로 우선순위를 지정한 @ControllerAdvice에
primary root exception mapping을 선언하는 것을 권장합니다. 자세한 내용은 the MVC section을 참조하십시오.
WebFlux에서의
@ExceptionHandlermethod는 request body 및@ModelAttribute관련 method argument를 예외로 하고,@RequestMappingmethod와 동일한 method argument와 return value를 지원합니다.
Spring WebFlux에서 @ExceptionHandler method에 대한 지원은 @RequestMapping method용
HandlerAdapter에 의해 제공됩니다. 자세한 내용은 DispatcherHandler를 참조하십시오.
See equivalent in the Servlet stack
exception type 외에도, @ExceptionHandler method는 producible media type을 선언할 수 있습니다.
이는 HTTP client가 요청한 media type(일반적으로 "Accept" HTTP request header)을 기반으로 error response를 세분화할 수 있게 해줍니다.
애플리케이션은 동일한 exception type에 대해 annotation에 직접 producible media type을 선언할 수 있습니다:
1@ExceptionHandler(produces = "application/json") 2public ResponseEntity<ErrorMessage> handleJson(IllegalArgumentException exc) { 3 return ResponseEntity.badRequest().body(new ErrorMessage(exc.getMessage(), 42)); 4} 5 6@ExceptionHandler(produces = "text/html") 7public String handle(IllegalArgumentException exc, Model model) { 8 model.addAttribute("error", new ErrorMessage(exc.getMessage(), 42)); 9 return "errorView"; 10}
1@ExceptionHandler(produces = ["application/json"]) 2fun handleJson(exc: IllegalArgumentException): ResponseEntity<ErrorMessage> { 3 return ResponseEntity.badRequest().body(ErrorMessage(exc.message, 42)) 4} 5 6@ExceptionHandler(produces = ["text/html"]) 7fun handle(exc: IllegalArgumentException, model: Model): String { 8 model.addAttribute("error", ErrorMessage(exc.message, 42)) 9 return "errorView" 10}
여기서 method는 동일한 exception type을 처리하지만, duplicate로 거부되지 않습니다. 대신 "application/json"을 요청하는 API client는 JSON error를 받고, browser는 HTML error view를 받게 됩니다.
각 @ExceptionHandler annotation은 여러 개의 producible media type을 선언할 수 있으며,
error handling phase 동안의 content negotiation이 어떤 content type이 사용될지 결정합니다.
See equivalent in the Servlet stack
@ExceptionHandler method는 request body가 이미 consume되었을 수 있다는 점을 제외하면,
@RequestMapping method와 동일한 method argument를 지원합니다.
See equivalent in the Servlet stack
@ExceptionHandler method는 @RequestMapping method와 동일한 return value를 지원합니다.
Validation
Controller Advice