-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: 성공 응답 공통 처리 어드바이스 생성 * remove: Getter 제거
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/main/java/com/depromeet/global/config/response/GlobalResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/depromeet/global/config/response/GlobalResponseAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |