본문 바로가기
JAVA/SpringBoot

[SpringBoot] Validation (유효성 검사/데이터 검증)

by 설총이 2023. 7. 27.
1. @annotation 종류

 

2. Maven Dependency 설정

 

3. DTO annotation 추가

 

4. Validation 테스트 수행
컨트롤러 메소드 내 @Valid 와 DTO 객체 내의 @annotation 과 연결되어 실패처리함.

 

5. Swagger saveProduct 실행

 

6. DefaultHandlerExceptionResolver LOG 확인

[2023-07-27 16:38:01.169] [WARN ] [http-nio-8080-exec-1] o.s.w.s.m.s.DefaultHandlerExceptionResolver Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public org.springframework.http.ResponseEntity<com.seypak.study.intellij.data.dto.ProductDto> com.seypak.study.intellij.controller.ProductController.createProduct(com.seypak.study.intellij.data.dto.ProductDto) with 2 errors:

[Field error in object 'productDto' on field 'productName': rejected value [null]; codes [NotEmpty.productDto.productName,NotEmpty.productName,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [productDto.productName,productName]; arguments []; default message [productName]]; default message [비어 있을 수 없습니다]]

[Field error in object 'productDto' on field 'productId': rejected value []; codes [NotBlank.productDto.productId,NotBlank.productId,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [productDto.productId,productId]; arguments []; default message [productId]]; default message [공백일 수 없습니다]] ]

'JAVA > SpringBoot' 카테고리의 다른 글

[SpringBoot] JUnit 테스트코드 - Controller  (0) 2023.07.31
[SpringBoot] JUnit 설명  (0) 2023.07.28
[SpringBoot] Logback 설정  (0) 2023.07.27
[SpringBoot] (Chapter1) ORM, JPA, Spring Data JPA  (0) 2023.07.26
[SpringBoot] 서비스 구조  (0) 2023.07.25