본문 바로가기

Web/spring

[Spring framework Web MVC docs] 1.8. Error Responses

728x90

틀린 해석이 있다면 알려주세요

https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-rest-exceptions


 

 

https://www.rfc-editor.org/rfc/rfc7807.html

 

RFC 7807: Problem Details for HTTP APIs

 

www.rfc-editor.org

 

A common requirement for REST services is to include details in the body of error responses. The Spring Framework supports the "Problem Details for HTTP APIs" specification, RFC 7807.

 

주요 추상화 리스트

  • ProblemDetail — representation for an RFC 7807 problem detail; a simple container for both standard fields defined in the spec, and for non-standard ones.
  • ErrorResponse — contract to expose HTTP error response details including HTTP status, response headers, and a body in the format of RFC 7807; this allows exceptions to encapsulate and expose the details of how they map to an HTTP response. All Spring MVC exceptions implement this.
  • ErrorResponseException — basic ErrorResponse implementation that others can use as a convenient base class.
  • ResponseEntityExceptionHandler — convenient base class for an @ControllerAdvice that handles all Spring MVC exceptions, and any ErrorResponseException, and renders an error response with a body.

 

 

1.8.1. Render

 
@ExceptionHandler, @RequestMapping 메소드에서
ProblemDetail 또는 ErrorResponse 를 리턴 받을 수 있다.
  • The status property of ProblemDetail determines the HTTP status.
  • The instance property of ProblemDetail is set from the current URL path, if not already set.
  • For content negotiation, the Jackson HttpMessageConverter prefers "application/problem+json" over "application/json" when rendering a ProblemDetail, and also falls back on it if no compatible media type is found.

 

 

ErrorResponseException, extend ResponseEntityExceptionHandler and declare it as an @ControllerAdvice in Spring configuration. 

확장하기 위해서는 @ControlleAdvice 에서 가능하다.

 

모든 예외를 ProblemDetail 에 매핑할 수 있고

예외를 처리하기 위한 @Exceptionhandler 가 있다

 

 

1.8.2. Non-Standard Fields

또 비표준필드로 확장할 수 있다

 

One, insert into the "properties" Map of ProblemDetail. When using the Jackson library, the Spring Framework registers ProblemDetailJacksonMixin that ensures this "properties" Map is unwrapped and rendered as top level JSON properties in the response, and likewise any unknown property during deserialization is inserted into this Map.

Map 을 사용하거나

 

 

위에 말했듯이 ProblemDetail 을 확장하는 방법이다. (@ControllerAdvice에서)

 

 

 

 

1.8.3. Internationalization

오류 응답 세부 정보는 국제화 해야한다.

 

ErrorResponse 의 message code와 arguments 를 이용하여 MessageSource 의 세부사항 필드에서 확인할 수 있다

ErrorResponse 

 

또한 "title" 필드 처리를 위해 ErrorResponse  에 메세지 코드를 보여주고

ResponseEntityExceptionHandler 를 이용하여 detail, title 필드를 처리해준다

 

기본적인 detail 필드 메세지 코드는 problemDetail

그리고 아래의 메세지Arguments 및 코드가 있다

 

ExceptionMessage CodeMessage Code Arguments

AsyncRequestTimeoutException (default)  
ConversionNotSupportedException (default) {0} property name, {1} property value
HttpMediaTypeNotAcceptableException (default) {0} list of supported media types
HttpMediaTypeNotAcceptableException (default) + ".parseError"  
HttpMediaTypeNotSupportedException (default) {0} the media type that is not supported, {1} list of supported media types
HttpMediaTypeNotSupportedException (default) + ".parseError"  
HttpMessageNotReadableException (default)  
HttpMessageNotWritableException (default)  
HttpRequestMethodNotSupportedException (default) {0} the current HTTP method, {1} the list of supported HTTP methods
MethodArgumentNotValidException (default) {0} the list of global errors, {1} the list of field errors. Message codes and arguments for each error within the BindingResult are also resolved via MessageSource.
MissingRequestHeaderException (default) {0} the header name
MissingServletRequestParameterException (default) {0} the request parameter name
MissingMatrixVariableException (default) {0} the matrix variable name
MissingPathVariableException (default) {0} the path variable name
MissingRequestCookieException (default) {0} the cookie name
MissingServletRequestPartException (default) {0} the part name
NoHandlerFoundException (default)  
TypeMismatchException (default) {0} property name, {1} property value
UnsatisfiedServletRequestParameterException (default) {0} the list of parameter conditions

By default, the message code for the "title" field is "problemDetail.title." + the fully qualified exception class name.

 

 

1.8.4. Client Handling

 

Client 에서 발생하였을 경우에는

RestClientResponseException 으로 잡을 수 있다.

 

getResponseBodyAs methods to decode the error response body to any target type such as ProblemDetail, or a subclass of ProblemDetail.

728x90