-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REFACTOR(wrapper) :: ResponseEntity -> 공통 Response로 처리
- Loading branch information
1 parent
4e337cc
commit b6c8686
Showing
8 changed files
with
160 additions
and
34 deletions.
There are no files selected for viewing
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
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
55 changes: 55 additions & 0 deletions
55
src/main/java/com/woongeya/zoing/global/converter/CommonHttpsMessageConverter.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,55 @@ | ||
package com.woongeya.zoing.global.converter; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.http.HttpInputMessage; | ||
import org.springframework.http.HttpOutputMessage; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.AbstractHttpMessageConverter; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.http.converter.HttpMessageNotWritableException; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StreamUtils; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.woongeya.zoing.global.converter.exception.NotSupportedException; | ||
import com.woongeya.zoing.global.wraper.response.CommonResponse; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
public class CommonHttpsMessageConverter extends AbstractHttpMessageConverter<CommonResponse<Object>> { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
public List<MediaType> getSupportedMediaTypes() { | ||
return Collections.singletonList(MediaType.APPLICATION_JSON); | ||
} | ||
|
||
@Override | ||
protected boolean supports(Class<?> clazz) { | ||
return clazz.equals(String.class); | ||
} | ||
|
||
@Override | ||
protected CommonResponse<Object> readInternal(Class<? extends CommonResponse<Object>> clazz, | ||
HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { | ||
throw new NotSupportedException(); | ||
} | ||
|
||
@Override | ||
protected void writeInternal(CommonResponse<Object> resultMessage, HttpOutputMessage outputMessage) throws | ||
IOException, | ||
HttpMessageNotWritableException { | ||
String responseMessage = this.objectMapper.writeValueAsString(resultMessage); | ||
StreamUtils.copy(responseMessage.getBytes(StandardCharsets.UTF_8), outputMessage.getBody()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/woongeya/zoing/global/converter/exception/NotSupportedException.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,12 @@ | ||
package com.woongeya.zoing.global.converter.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.woongeya.zoing.global.exception.JJoingException; | ||
|
||
public class NotSupportedException extends JJoingException { | ||
|
||
public NotSupportedException() { | ||
super(HttpStatus.INTERNAL_SERVER_ERROR, "지원하지 않는 형식입니다."); | ||
} | ||
} |
16 changes: 6 additions & 10 deletions
16
src/main/java/com/woongeya/zoing/global/exception/ErrorResponse.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
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
38 changes: 38 additions & 0 deletions
38
src/main/java/com/woongeya/zoing/global/wraper/ResponseWrapper.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,38 @@ | ||
package com.woongeya.zoing.global.wraper; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.HttpStatus; | ||
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; | ||
|
||
import com.woongeya.zoing.global.exception.ErrorResponse; | ||
import com.woongeya.zoing.global.wraper.response.CommonResponse; | ||
|
||
@RestControllerAdvice | ||
public class ResponseWrapper implements ResponseBodyAdvice<Object> { | ||
|
||
@Override | ||
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object beforeBodyWrite(Object body, | ||
MethodParameter returnType, | ||
MediaType selectedContentType, | ||
Class<? extends HttpMessageConverter<?>> selectedConverterType, | ||
ServerHttpRequest request, | ||
ServerHttpResponse response | ||
) { | ||
if (body instanceof ErrorResponse errorResponse) { | ||
response.setStatusCode(errorResponse.status()); | ||
return CommonResponse.error(errorResponse.status(), errorResponse.message()); | ||
} | ||
|
||
return CommonResponse.success(HttpStatus.OK, body); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/woongeya/zoing/global/wraper/response/CommonResponse.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,21 @@ | ||
package com.woongeya.zoing.global.wraper.response; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public record CommonResponse<T> ( | ||
HttpStatus state, | ||
T body, | ||
LocalDateTime timeStamp, | ||
String message | ||
) { | ||
|
||
public static <T> CommonResponse<T> error(HttpStatus state, String message) { | ||
return new CommonResponse<>(state, null, LocalDateTime.now(), message); | ||
} | ||
|
||
public static <T> CommonResponse<T> success(HttpStatus state, T body) { | ||
return new CommonResponse<>(state, body, LocalDateTime.now(), ""); | ||
} | ||
} |