Skip to content

Commit 4d64cf2

Browse files
committed
refactor: 커스텀 잭슨 컨버터 필터로 대체
ContentCachingRequestWrapper로 내부의 스트림 캐싱하도록 변경
1 parent ce82d2c commit 4d64cf2

File tree

3 files changed

+34
-41
lines changed

3 files changed

+34
-41
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.imd.yourvoice.common;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.web.filter.OncePerRequestFilter;
5+
import org.springframework.web.util.ContentCachingRequestWrapper;
6+
import org.springframework.web.util.ContentCachingResponseWrapper;
7+
8+
import javax.servlet.FilterChain;
9+
import javax.servlet.ServletException;
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
import java.io.IOException;
13+
14+
@Configuration
15+
public class CachingBodyPerRequestFilter extends OncePerRequestFilter {
16+
17+
@Override
18+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
19+
ContentCachingRequestWrapper cachingRequestWrapper = new ContentCachingRequestWrapper(request);
20+
ContentCachingResponseWrapper cachingResponseWrapper = new ContentCachingResponseWrapper(response);
21+
22+
filterChain.doFilter(cachingRequestWrapper, cachingResponseWrapper);
23+
cachingResponseWrapper.copyBodyToResponse();
24+
}
25+
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
package com.imd.yourvoice.common;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import lombok.SneakyThrows;
35
import org.springframework.http.HttpStatus;
46
import org.springframework.http.converter.HttpMessageNotReadableException;
57
import org.springframework.web.bind.MethodArgumentNotValidException;
68
import org.springframework.web.bind.annotation.ExceptionHandler;
79
import org.springframework.web.bind.annotation.ResponseStatus;
810
import org.springframework.web.bind.annotation.RestControllerAdvice;
11+
import org.springframework.web.util.ContentCachingRequestWrapper;
912

10-
import javax.servlet.http.HttpServletRequest;
13+
import java.util.HashMap;
1114
import java.util.Map;
1215

1316
@RestControllerAdvice
1417
public class CommonExceptionHandler {
1518

1619
@ResponseStatus(HttpStatus.BAD_REQUEST)
1720
@ExceptionHandler(MethodArgumentNotValidException.class)
18-
public ResponseDTO<Map<String, Object>> methodArgumentNotValidExceptionHandler(HttpServletRequest request) {
21+
public ResponseDTO<Map<String, Object>> methodArgumentNotValidExceptionHandler(ContentCachingRequestWrapper request) {
1922
return ResponseDTO.<Map<String, Object>>builder()
2023
.data(getRequestBodyAt(request))
2124
.message("Validation Error")
@@ -25,15 +28,16 @@ public ResponseDTO<Map<String, Object>> methodArgumentNotValidExceptionHandler(H
2528

2629
@ResponseStatus(HttpStatus.BAD_REQUEST)
2730
@ExceptionHandler(HttpMessageNotReadableException.class)
28-
public ResponseDTO<Map<String, Object>> httpMessageNotReadableExceptionHandler(HttpServletRequest request) {
31+
public ResponseDTO<Map<String, Object>> httpMessageNotReadableExceptionHandler(ContentCachingRequestWrapper request) {
2932
return ResponseDTO.<Map<String, Object>>builder()
3033
.data(getRequestBodyAt(request))
3134
.message("UnknownRequestProperties Error")
3235
.isSuccess("fail")
3336
.build();
3437
}
3538

36-
private Map<String, Object> getRequestBodyAt(HttpServletRequest request) {
37-
return (Map<String, Object>) request.getAttribute("requestBody");
39+
@SneakyThrows
40+
private Map<String, Object> getRequestBodyAt(ContentCachingRequestWrapper request) {
41+
return new ObjectMapper().readValue(request.getContentAsByteArray(), HashMap.class);
3842
}
3943
}

src/main/java/com/imd/yourvoice/config/MappingJackson2HttpMessageConverterWithCachingRequest.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)