import org.springframework.boot.web.error.ErrorAttributeOptions
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes
import org.springframework.stereotype.Component
import org.springframework.web.ErrorResponseException
import org.springframework.web.context.request.WebRequest
@Component
class CustomErrorAttributes: DefaultErrorAttributes() {
override fun getErrorAttributes(webRequest: WebRequest?, options: ErrorAttributeOptions?): MutableMap<String, Any> {
val attributes = super.getErrorAttributes(webRequest, options)
val error = getError(webRequest)
if (error is ErrorResponseException && error.body.properties != null) {
attributes.putAll(error.body.properties!!)
}
return attributes
}
}
단점 2) GET 메서드인 경우 브라우저에서 접근시 /error 매핑으로 이동하므로 아래와 같은 에러 화면이 보인다.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Apr 02 18:57:04 KST 2024
There was an unexpected error (type=Locked, status=423).
아래와 같이 비활성화 시킬 수 있지만, 그러면 톰캣 정보가 출력 됨 (ResponseStatusException을 반환한 경우)
@RestControllerAdvice(annotations = RestController.class)
public class GlobalRestControllerAdvice {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(UserAlreadyExistsException::class)
fun handle(e: UserAlreadyExistsException): ProblemDetail {
@RestControllerAdvice(annotations = RestController.class)
public class GlobalRestControllerAdvice {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(UserAlreadyExistsException::class)
fun handle(e: UserAlreadyExistsException): ProblemDetail {