[Spring] @Valid Annotation

Entity

애노테이션을 사용하면 쉽게 Entity에 대한 유효성 검사를 할 수 있다. 해당 애노테이션은 javax.validation.constraints패키지에 정의되어 있으며, 이를 아래와 같이 활용할 수 있다.


@Data
public class Member {

 private Long idx;
 
 @NotNull(message="name null")
 private String name;
 
 @Min(value=14, message="min 14")
 private Integer age;
 
 @NotNull(message="tel null")
 private String tel;
}

@NotNull: null 검증
@Min@Max: 최소값, 최대값 검증
@Size: 범위 검증
@Email: e-mail 검증
@AssertTrue: true 검증


@NotEmpty: null이나 size가 0 검증 (String, Collection)
@NotBlank: null이나 whitespace 검증 (String)
@Positive@PositiveOrZero: 숫자 검증
@Negative@NegativeOrZero: 숫자 검증
@Past@PastOrPresent: 날짜 검증
@Future@FutureOrPresent: 날짜 검증

댓글

이 블로그의 인기 게시물

[Protocol] WIEGAND 통신

Orange for Oracle에서 한글 깨짐 해결책

[URL] 대소문자를 구분하나?