Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 스프링 핵심 원리
- AOP
- 알고리즘
- http
- springdatajpa
- QueryDSL
- JDBC
- Servlet
- pointcut
- 인프런
- 백준
- Proxy
- jpa
- Spring Boot
- transaction
- Android
- SpringBoot
- kotlin
- JPQL
- 김영한
- db
- 스프링 핵심 기능
- 스프링
- Greedy
- Exception
- Thymeleaf
- java
- spring
- 자바
- 그리디
Archives
- Today
- Total
개발자되기 프로젝트
Bean Validation - 시작 본문
1. Bean Validation 의존관계 추가
- build.gradle
implementation 'org.springframework.boot:spring-boot-starter-validation'
- 추가되는 라이브러리
- jakarta.validation-api : Bean Validation 인터페이스
- hibernate-validator : 구현체
2. Bean Validation 사용
import lombok.Data;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Data
public class Item {
private Long id;
@NotBlank
private String itemName;
@NotNull
@Range(min=1000, max=1000000)
private Integer price;
@NotNull
@Max(9999)
private Integer quantity;
public Item() {
}
public Item(String itemName, Integer price, Integer quantity) {
this.itemName = itemName;
this.price = price;
this.quantity = quantity;
}
}
- javax.validation.constraints.NotNull
- org.hibernate.validator.constraints.Range
- javax로 시작하는 validation은 java 표준으로 모든 구현체에서 사용 가능.
- org.hibernate 로 시작하는 validation은 hibernage Validator 구현체를 사용할 때만 사용 가능.
- 근데 대부분 hibernate validator 사용함 ㅋㅋ
3. Test
- 검증기 생성
- 이후 스프링과 통합하면 우리가 직접 이런 코드 필요 없음.
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
- 검증 실행
- 검증 대상( item )을 직접 검증기에 넣고 그 결과를 받음.
- Set 에는 ConstraintViolation 이라는 검증오류가 담긴다.
- 따라서 결과가 비어있으면 검증 오류가 없음.
Set<ConstraintViolation<Item>> violations = validator.validate(item);
- Test
@Test
void beanValidation(){
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Item item = new Item();
item.setItemName(" ");
item.setPrice(0);
item.setQuantity(10000);
Set<ConstraintViolation<Item>> violations = validator.validate(item);
for (ConstraintViolation<Item> violation : violations) {
System.out.println("violation = " + violation);
System.out.println("violation = " + violation.getMessage());
}
}
- 실행 결과
violation = ConstraintViolationImpl{interpolatedMessage='9999 이하여야 합니다', propertyPath=quantity, rootBeanClass=class hello.itemservice.domain.item.Item, messageTemplate='{javax.validation.constraints.Max.message}'}
violation = 9999 이하여야 합니다
violation = ConstraintViolationImpl{interpolatedMessage='공백일 수 없습니다', propertyPath=itemName, rootBeanClass=class hello.itemservice.domain.item.Item, messageTemplate='{javax.validation.constraints.NotBlank.message}'}
violation = 공백일 수 없습니다
4. GitHub : 210925 Bean Validation
GitHub - bsh6463/Validation-V1
Contribute to bsh6463/Validation-V1 development by creating an account on GitHub.
github.com
'인프런 > [인프런] 스프링 MVC 2' 카테고리의 다른 글
Bean Validation - Error Code (0) | 2021.09.25 |
---|---|
Bean Validation - Spring 적용 (0) | 2021.09.25 |
Bean Validation (0) | 2021.09.25 |
Validator 분리 2 (0) | 2021.09.24 |
Validator 분리 1 (0) | 2021.09.24 |
Comments