Skip to content

Commit

Permalink
chore: 성공 응답 공통 처리 어드바이스 생성 (#34)
Browse files Browse the repository at this point in the history
* chore: 성공 응답 공통 처리 어드바이스 생성

* remove: Getter 제거
  • Loading branch information
kdomo authored Dec 1, 2023
1 parent 1d1c7b8 commit 50d3e36
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.depromeet.global.config.response;

import java.time.LocalDateTime;

public record GlobalResponse(boolean success, int status, Object data, LocalDateTime timestamp) {
public static GlobalResponse of(int status, Object data) {
return new GlobalResponse(true, status, data, LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.depromeet.global.config.response;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@RestControllerAdvice
public class GlobalResponseAdvice implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;
}

@Override
public Object beforeBodyWrite(
Object body,
MethodParameter returnType,
MediaType selectedContentType,
Class selectedConverterType,
ServerHttpRequest request,
ServerHttpResponse response) {
HttpServletResponse servletResponse =
((ServletServerHttpResponse) response).getServletResponse();
int status = servletResponse.getStatus();
HttpStatus resolve = HttpStatus.resolve(status);
if (resolve == null) {
return body;
}
if (resolve.is2xxSuccessful()) {
return GlobalResponse.of(status, body);
}
return body;
}
}

0 comments on commit 50d3e36

Please sign in to comment.