본문 바로가기
개발자의 정보/Java & framework

spring-framework 에서 error 응답 json 으로보내기

by pastory 2023. 2. 28.

스프링 프레임워크 (스프링부트 포함) 에서 REST API 서비스 구축할 때 에러페이지로 리디렉션 되는 경우가 있다.

이럴 때는 에러 처리 부분을 수정해 줄 수 있다. 각종 리졸버를 통한 방법 ResponseStatusEntity 관련한 방법 외에도 여러 방법이 있지만.. 가장 쉽고 빠르게 적용할 수 있는 방법이 있다.

import org.springframework.web.bind.annotation.RestControllerAdvice
import org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler

@RestControllerAdvice
class RestResponseExceptionHandler : ResponseEntityExceptionHandler() {

}

이게 끝이다. 이 얼마나 심플하고 깔끔하며 아름다운가. 찬양하라.

댓글