springboot_validation : bean validation
This commit is contained in:
@@ -21,6 +21,7 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
|
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||||
compileOnly 'org.projectlombok:lombok'
|
compileOnly 'org.projectlombok:lombok'
|
||||||
annotationProcessor 'org.projectlombok:lombok'
|
annotationProcessor 'org.projectlombok:lombok'
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
|
|||||||
@@ -1,13 +1,26 @@
|
|||||||
package hello.itemservice.domain.item;
|
package hello.itemservice.domain.item;
|
||||||
|
|
||||||
import lombok.Data;
|
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
|
@Data
|
||||||
public class Item {
|
public class Item {
|
||||||
|
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
|
@NotBlank
|
||||||
private String itemName;
|
private String itemName;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Range(min = 1000, max = 1000000)
|
||||||
private Integer price;
|
private Integer price;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Max(9999)
|
||||||
private Integer quantity;
|
private Integer quantity;
|
||||||
|
|
||||||
public Item() {
|
public Item() {
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package hello.itemservice.validation;
|
||||||
|
|
||||||
|
import hello.itemservice.domain.item.Item;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.Validation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import javax.validation.ValidatorFactory;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class BeanValidationTest {
|
||||||
|
|
||||||
|
@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>> validations = validator.validate(item);
|
||||||
|
validations.forEach(System.out::println);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user