728x90

1. validation을 활성화하기위해 디펜던시를 추가하고,
@RestControllerAdvice
를 선언한 클래스에 handleConstraintViolation예외처리를 하였는데 에러응답이 다른 응답으로 나왔습니다.
<!--validation-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
@ExceptionHandler(value = {ConstraintViolationException.class})
protected ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex) {
ErrorCode errorCode = ErrorCode.INVALID_BAD_REQUEST;
ExceptionResponse exceptionResponse = ExceptionResponse.builder()
.success(false)
.httpStatus(errorCode.getHttpStatus().value())
.httpStatusCode(errorCode.getHttpStatus().name())
.value(errorCode.getValue())
.code(errorCode.name())
.msg(ex.getMessage())
.build();
return ResponseEntity.status(errorCode.getHttpStatus()).body(exceptionResponse);
}

해결방법: Controller에 @Validated 어노테이션을 추가해야합니다.
ControllerAdvice 의 사용자 지정 구현이 되도록 하였습니다.
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/api/order")
public class OrderController {
해결! 아래처럼 정상적으로 나온것을 확인했습니다.

'Trouble Shooting' 카테고리의 다른 글
| [Spring Boot] RequestBody @Valid ExceptionHandle (0) | 2023.09.21 |
|---|---|
| [Git] remote: Invalid username or password. (0) | 2023.09.15 |
| [Linux] FirewallD is not running (0) | 2023.09.08 |
| filzila로 전송이 안됄때 (0) | 2023.09.05 |
| [Spring Boot] Unsatisfied dependency expressed through field '..'; 단위 테스트시 Bean 생성에러 (0) | 2023.08.14 |