티스토리 뷰

개발

bean validation에 대해서

달리는개발자 2019. 3. 13. 23:37

Bean Validation은 자바의 객체 검증을 위한 표준

Version 

 JSR

 Release

 Hibernate Validator Compatible

  Bean Validation 1.0

 JSR 303

 2009년, Jave EE6

 hibernate-validator 4.3.1.Final

  Bean Validation 1.1

 JSR 349

 2013년, Jave EE7

 hibernate-validator 5.1.1.Final

  Bean Validation 2.0

 JSR 380

 2017년 8월 Jave EE8

 hibernate-validator 6.0.1.Final


2.0은 인증된 구현체로 hibernate-validator 6.0.1.Final 버전 이상이다.

1.1과 2.0의 주요 변경사항은 다음과 같다.

  • support for validating container elements by annotating type arguments of parameterized types e.g. List<@Positive Integer> positiveNumbers. This also includes:

    • more flexible cascaded validation of container types

    • support for java.util.Optional

    • support for the property types declared by JavaFX

    • support for custom container types

  • support for the new date/time data types (JSR 310) for @Past and @Future

  • new built-in constraints: @Email@NotEmpty@NotBlank@Positive@PositiveOrZero@Negative@NegativeOrZero@PastOrPresent and@FutureOrPresent

  • leverage the JDK 8 new features (built-in constraints are marked repeatable, parameter names are retrieved via reflection)


전체 변경 사항 보기


@NotEmpty, @NotBlank의 차이는 "          " 빈 문자열인 경우 @NotBlank로 설정했을 때만 위반임.

@Positive, @Negative를 통해서 양수 음수 검증이 가능함.

@Email를 통한 편리한 이메일 패턴 검증

@Pattern을 이용해서 regular expression으로 검증

@Past, @Future를 이용해 현재보다 전인지 후인지 날짜 검증


Spring Boot 2.0.0.RELEASE 버전 기본 dependency는 hibernate-validator 6.0.7.Final 이다.


문제해결


문제

class Person {

    @NotEmpty

    private Long personNo;

}


javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.lang.Long'. Check configuration for 'personNo'


원인

올바르지 않은 타입에 애노테이션을 붙인 경우


해결

@NotEmpty 를 @NotNull로 변경


참고

https://beanvalidation.org/specification/

https://www.baeldung.com/bean-validation-container-elements

https://www.popit.kr/javabean-validation%EA%B3%BC-hibernate-validator-%EA%B7%B8%EB%A6%AC%EA%B3%A0-spring-boot/

https://github.com/spring-projects/spring-boot/blob/v2.0.0.RELEASE/spring-boot-project/spring-boot-dependencies/pom.xml

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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 29 30 31
글 보관함