Skip to content

feat: 공통응답으로 감싸는 어드바이스 추가 #6

Merged
yunjae62 merged 2 commits into
mainfrom
feat/add-common-response-advice
Aug 3, 2025
Merged

feat: 공통응답으로 감싸는 어드바이스 추가 #6
yunjae62 merged 2 commits into
mainfrom
feat/add-common-response-advice

Conversation

@yunjae62

@yunjae62 yunjae62 commented Aug 3, 2025

Copy link
Copy Markdown
Owner

개요

  • 기존의 코드는 매번 공통응답 클래스로 감싸주어야 했다. 이러한 보일러 플레이트를 제거하고자 응답 DTO를 자동으로 공통응답으로 감싸주는 어드바이스를 추가하였다.

작업사항

  • ApiResponseAdvice 추가
    • 컨트롤러의 보일러 플레이트를 제거하는데 의의를 두고자, 이미 ApiResponse로 응답되는 에외처리 응답의 경우는 그대로 응답하기로 함
  • 기존의 컨트롤러에서 작성된 보일러 플레이트 제거

세부사항

  • ApiResponseAdvice 추가
    import org.springframework.core.MethodParameter;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.server.ServerHttpRequest;
    import org.springframework.http.server.ServerHttpResponse;
    import org.springframework.web.bind.annotation.RestControllerAdvice;
    import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
    
    /**
     * 공통응답으로 감싸주는 어드바이스
     */
    @RestControllerAdvice
    public class ApiResponseAdvice implements ResponseBodyAdvice<Object> {
    
        /**
         * 실패 응답 (GlobalExceptionHandler error 처리가 된 응답)은 이미 ApiResponse 감싸져서 해당되지 않음
         */
        @Override
        public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
            return !returnType.getParameterType().equals(ApiResponse.class);
        }
    
        @Override
        public Object beforeBodyWrite(
            Object body,
            MethodParameter returnType,
            MediaType selectedContentType,
            Class<? extends HttpMessageConverter<?>> selectedConverterType,
            ServerHttpRequest request,
            ServerHttpResponse response
        ) {
            // 빈 응답은 기본 응답으로 처리
            if (body == null || returnType.getParameterType().equals(Void.TYPE)) {
                return ApiResponse.success();
            }
    
            return ApiResponse.success(body); // 성공 응답에 공통 응답 처리
        }
    }
  • 기존의 컨트롤러에서 공통 응답 제거 (메서드의 리턴 타입을 보시면 됩니다.)
    /* 기존 컨트롤러 */
    
    // (1)
    @GetMapping("/{id}")
    public ApiResponse<GetSampleResponse> getSample(@PathVariable("id") Long id) { 
        return ApiResponse.success(sampleQueryService.getSample(id));
    }
    
    // (2)
    @PostMapping("/signup")
    public ApiResponse<EmptyResponse> signup(
        @Valid @RequestBody UserSignupRequest request
    ) {
        userAuthService.signup(request);
        return ApiResponse.success();
    }
    
    /* 변경 컨트롤러 */
    
    // (1)
    @GetMapping("/{id}")
    public GetSampleResponse getSample(@PathVariable("id") Long id) {
        return sampleQueryService.getSample(id);
    }
    
    // (2)
    @PostMapping("/signup")
    public void signup(
        @Valid @RequestBody UserSignupRequest request
    ) {
        userAuthService.signup(request);
    }

관련 이슈

@github-actions

github-actions Bot commented Aug 3, 2025

Copy link
Copy Markdown

Test Results

1 tests   1 ✅  0s ⏱️
1 suites  0 💤
1 files    0 ❌

Results for commit 6a7abe6.

@yunjae62 yunjae62 merged commit b2b0e2d into main Aug 3, 2025
2 checks passed
@yunjae62 yunjae62 deleted the feat/add-common-response-advice branch August 3, 2025 04:05
@yunjae62 yunjae62 self-assigned this Aug 5, 2025
@yunjae62 yunjae62 added the enhancement New feature or request label Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant