11package com .imd .yourvoice .common ;
22
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import lombok .SneakyThrows ;
35import org .springframework .http .HttpStatus ;
46import org .springframework .http .converter .HttpMessageNotReadableException ;
57import org .springframework .web .bind .MethodArgumentNotValidException ;
68import org .springframework .web .bind .annotation .ExceptionHandler ;
79import org .springframework .web .bind .annotation .ResponseStatus ;
810import 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 ;
1114import java .util .Map ;
1215
1316@ RestControllerAdvice
1417public 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}
0 commit comments