복붙노트

[SPRING] Spring Boot Rest Controller에서 기본 JSON 오류 응답 수정

SPRING

Spring Boot Rest Controller에서 기본 JSON 오류 응답 수정

현재 스프링 부트의 오류 응답에는 다음과 같은 표준 컨텐트가 들어 있습니다.

{
   "timestamp" : 1426615606,
   "exception" : "org.springframework.web.bind.MissingServletRequestParameterException",
   "status" : 400,
   "error" : "Bad Request",
   "path" : "/welcome",
   "message" : "Required String parameter 'name' is not present"
}

나는 응답에서 "예외"속성을 없애는 방법을 찾고있다. 이것을 달성 할 수있는 방법이 있습니까?

해결법

  1. ==============================

    1.에러 핸들링에 대한 문서에서 설명한 것처럼, ErrorAttributes를 구현 한 자신 만의 bean을 제공하여 컨텐츠를 제어 할 수 있습니다.

    에러 핸들링에 대한 문서에서 설명한 것처럼, ErrorAttributes를 구현 한 자신 만의 bean을 제공하여 컨텐츠를 제어 할 수 있습니다.

    쉬운 방법은 DefaultErrorAttributes를 서브 클래 싱하는 것입니다. 예 :

    @Bean
    public ErrorAttributes errorAttributes() {
        return new DefaultErrorAttributes() {
            @Override
            public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
                Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
                // Customize the default entries in errorAttributes to suit your needs
                return errorAttributes;
            }
    
       };
    }
    
  2. from https://stackoverflow.com/questions/29106637/modify-default-json-error-response-from-spring-boot-rest-controller by cc-by-sa and MIT license