content: package org.sopt.seminar2.common;
  
  import org.sopt.seminar2.common.dto.ResponseDto;
  import org.springframework.core.MethodParameter;
  import org.springframework.http.MediaType;
  import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  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 ResponseDtoAdvice implements ResponseBodyAdvice<Object> {
  
      @Override
      public boolean supports(MethodParameter returnType, Class converterType) {
          return !(returnType.getParameterType() == ResponseDto.class)
                  && MappingJackson2HttpMessageConverter.class.isAssignableFrom(converterType);
      }
  
      @Override
      public Object beforeBodyWrite(
              Object body,
              MethodParameter returnType,
              MediaType selectedContentType,
              Class selectedConverterType,
              ServerHttpRequest request,
              ServerHttpResponse response
      ) {
          if (body instanceof ResponseDto<?>) {
              return body;
          } else {
              return ResponseDto.success(body);
          }
      }
  }
  