💻
Albert's Til
GitHub
  • 매일매일 조금씩 성장하기
    • README
    • CS
      • Network
      • HTTP
        • NO-CACHE
      • 오류 코드
      • ORM 도구
      • Design Pattern
        • CQRS Pattern
          • Event Sourcing and CQRS pattern
        • Builder Pattern
    • DB
      • MySQL
        • Timeline
        • Pagination
        • Index
        • Database Performance Optimization Strategies
        • B+ tree
        • MySQL Connectors VS MySQL Shell(Scripting) VS MySQL Workbench
        • MySQL Storage Engine Architecture
      • Normalization & Denormalization
      • JPA
        • @Transactional
        • Why JPA?
        • About JPA
        • N+1 Issue
        • Index
        • ElementCollection&CollectionTable
        • orphanRemoval
        • CascadeType
        • Use Subselect
        • Dynamic Instance Creation
        • Paging
        • Order
        • Spefication
        • mappedBy
      • MongoDB
        • ObjectId
      • Why MySQL?
      • ACID properties of transactions
      • Between JPA and JDBC
      • Identifiers in Hibernate/JPA
    • Java
      • Jackson de/serialize
      • Collections.singletonList() vs List.of()
      • Manage dependencies in Gradle
      • Logging Level
      • Bean Validation
      • JVM Internals
        • Threads
          • Frame
        • Shared Between Threads
          • Classloader
            • Class Loader Hierarchy
            • Loading Linking Initialization
      • Java Collection Framework
      • Annotation
      • Generic
      • 디미터 법칙
    • Spring
      • Caching
      • Spring Integration Overview
        • ThreadPollTaskExecutor
        • Messaging Bridge
        • Channel Adapter
        • Poller
        • Configuration and @EnableIntegration
        • Message Endpoints
        • Message Channels
      • HATEOAS
      • @Autowired vs Constructor Dependency Injection
      • Spring Security
        • JWT 토큰 사용한 인가
        • OAuth 2 Login
        • OAuth 2 인증
        • 인가
        • 인증
        • PasswordEncoder
      • IoC Container
      • Filter,Interceptor,AOP,Argument Resolver
      • Spring Annotation
      • About Spring
    • Kafka
      • Error Channel
    • Infra
      • Scale Up || Scale Out
      • Docker
        • Dockerfile
        • Docker Hub Deploy
        • Command
      • Cloud 유형
        • Infrastructure as a Service
        • Platform as a Service
        • Software as a Service
      • 무중단 배포
        • 엔진엑스(Nginx)
      • 코드 자동 배포
        • Technical
      • AWS EC2
        • PEM(Privacy Enhanced Mail) 키
      • AWS RDS
      • AWS S3
    • CodeSquad
      • Spring Boot Project 1주차 회고
      • Spring Boot Project 2주차 회고
      • Spirng Boot Project 3주차 회고
      • Spring Boot Project 4주차 회고
    • Foody Moody 프로젝트
      • Query Performance Comparison
      • HeartCount Asynchronous Issue
      • DeferredResult
      • ResponseBodyEmitter
      • SseEmitter (Spring)
      • Server-Sent Events (SSE)
      • 기술 스택 적용 이유
      • NO-CACHE(HTTP)
      • Transactional
    • DDD
      • AggregateId
    • Test
      • RestAssured
    • Coding and Algorithmic Problems
      • 819. Most Common Word
      • 344. Reverse String
      • 125. Valid Palindrome
      • 937. Reorder Data in Log Files
    • Node
      • Async... Await...
      • Custom Transactional Decorator Challenger
    • Python
      • Python Basic Grammar
        • Comments and Input/Output
        • Variable
        • Data type
        • Operations and syntax
        • List,Tuple,Dictionary,Set
        • Function
        • Conditional statement
        • Loop
    • HTML
      • HTML Basic
      • HTML Basic Tags
      • HTML Form Tags
      • HTML Table Tags
    • CSS
      • CSS Basic
      • CSS Practice
Powered by GitBook
On this page
  • JSR 380
  • Dependencies
  • Standard JSR annotations
  • Additional annotations
  • Creating a Composed Constraint
  • @ReportAsSingleViolation
  • @ConstraintComposition(CompositionType.OR)
  • @SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT)
  • Reference

Was this helpful?

  1. 매일매일 조금씩 성장하기
  2. Java

Bean Validation

표준 JSR-380 프레임워크 및 Jakarta Bean Validation 3.0

JSR 380

  • JSR 380은 Jakarta EE 및 JavaSE의 일부인 빈 유효성 검사를 위한 Java API의 사양입니다. 이렇게 하면 @NotNull, @Min 및 @Max와 같은 annotations을 사용하여 빈의 속성이 특정 기준을 충족하는지 확인할 수 있습니다.

  • 이 버전은 Hibernate-Validator 8.0.0을 제공하는 Spring Boot 3.x가 사용되므로 Java 17 이상이 필요합니다.

  • 또한 stream, Optional Improvements ,modules, private interface methods 등과 같이 Java 9 이상에 도입된 많은 새로운 기능을 지원합니다.

Dependencies

  • spring-boot-starter-validation (latest version)

  • hibernate-validator 8.0.0 Final

Standard JSR annotations

  • @NotNUll

    • Annotation이 달린 속성 값이 null 아닌지 확인합니다.

  • @AssertTrue

    • Annotation이 달린 속성 값이 참인지 확인합니다.

  • @Size

    • Annotation이 달린 속성 값의 크기가 min속성과 max속성 상이에 있는지 확인합니다. String,Collection,Map 및 배열 속성에 적용할 수 있습니다.

  • @Min

    • Annotation 속성의 value 속성보다 작지 않는 값이 있는지 확인합니다.

  • @Max

    • Annotation 속성의 value 속성보다 크지 않은 값이 있는지 확인합니다.

  • @Email

    • Annotation 속성이 유효한 이메일 주소인지 확인합니다.

Additional annotations

  • @NotEmpty

    • 속성이 null이거사 비어 있지 않은지 확인합니다. String, Collection, Map 또는 Array 값에 적용할 수 있습니다.

  • @NotBlank

    • 텍스트 값에만 적용할 수 있으며 속성이 null 또는 whitespace이 아닌지 확인합니다.

  • @Positive and @PositiveOrZero

    • 숫자 값에 적용하고 값이 양수인지 또는 0을 포함하여 양수인지 확인합니다.

  • @Negative and @NegativeOrZero

    • 숫자 값에 적용되며 값이 완전히 음수인지 또는 0을 포함하여 음수인지 확인합니다.

  • @Past 및 @PastOrPresent

    • 날짜 값이 과거 또는 현재를 포함한 과거인지 확인합니다. Java 8에 추가된 날짜 유형을 포함하여 날짜 유형에 적용할 수 있습니다.

  • @Future 및 @FutureOrPresent

    • 날짜 값이 미래인지 또는 현재를 포함한 미래인지 확인합니다.

Creating a Composed Constraint

@NotNull
@Pattern(regexp = ".*\\d.*", message = "must contain at least one numeric character")
@Length(min = 6, max = 32, message = "must have between 6 and 32 characters")
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {})
public @interface ValidAlphanumeric {

    String message() default "field should have a valid length and contain numeric character(s).";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

@ReportAsSingleViolation

  • 단일 ConstraintViolation을 반환하도록 유효성 검사를 진행한다.

@ConstraintComposition(CompositionType.OR)

  • 적어도 하나의 제약조건을 만족 하는지 확인한다.

@SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT)

  • 메서드의 반환 값을 확인한다.

Reference

Last updated 2 years ago

Was this helpful?

Constraint Composition with Bean Validation | BaeldungBaeldung
Java Bean Validation Basics | BaeldungBaeldung
Logo
Logo