From 071816b2135de9a538818926c9932994ed1de404 Mon Sep 17 00:00:00 2001 From: liyusang1 Date: Fri, 5 Jan 2024 20:22:17 +0900 Subject: [PATCH] =?UTF-8?q?[fix]=20GlobalExceptionRestAdvice=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GlobalExceptionRestAdvice 수정 --- .../global/exception/GlobalExceptionRestAdvice.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/haejwo/tripcometrue/global/exception/GlobalExceptionRestAdvice.java b/src/main/java/com/haejwo/tripcometrue/global/exception/GlobalExceptionRestAdvice.java index 6a81734f..2e02c309 100644 --- a/src/main/java/com/haejwo/tripcometrue/global/exception/GlobalExceptionRestAdvice.java +++ b/src/main/java/com/haejwo/tripcometrue/global/exception/GlobalExceptionRestAdvice.java @@ -1,7 +1,7 @@ package com.haejwo.tripcometrue.global.exception; import com.haejwo.tripcometrue.global.util.ResponseDTO; -import java.util.Map; +import java.util.List; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataAccessException; @@ -9,7 +9,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; -import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -66,14 +65,16 @@ public ResponseEntity> handleValidationExceptions( BindingResult bindingResult = e.getBindingResult(); - Map fieldErrors = bindingResult.getFieldErrors() + List fieldErrors = bindingResult.getFieldErrors() .stream() - .collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage)); + .map(error -> error.getField() + ": " + error.getDefaultMessage()) + .collect(Collectors.toList()); log.error(e.getMessage(), e); + String errorMessage = String.join(", ", fieldErrors); + return ResponseEntity .status(HttpStatus.BAD_REQUEST) - .body(ResponseDTO.errorWithMessage(HttpStatus.BAD_REQUEST, - fieldErrors.values().toString().substring(1,fieldErrors.values().toString().length()-1))); + .body(ResponseDTO.errorWithMessage(HttpStatus.BAD_REQUEST, errorMessage)); } }