JSR 303, 349, 380 (Bean Validation)

About

Bean Validation is a Java specification that defines a standard way to declare and validate constraints on object fields, method parameters, and return values using annotations. It plays a key role in Java SE and EE applications by helping enforce data integrity and input validation in a declarative and reusable way.

Bean Validation is defined through a series of JSRs:

  • JSR 303 – Bean Validation 1.0

  • JSR 349 – Bean Validation 1.1

  • JSR 380 – Bean Validation 2.0

Why Use Bean Validation?

  • Declarative Constraints: Use annotations like @NotNull, @Size, @Email to enforce validation rules.

  • Reusability: Write constraint logic once and apply it consistently across layers.

  • Integration: Automatically integrated with Java frameworks like:

    • Spring Boot

    • Jakarta EE (formerly Java EE) (e.g., JAX-RS, JPA, CDI)

    • Hibernate (via Hibernate Validator)

  • Interoperability: Works uniformly across tools and platforms due to standardization.

JSR Evolution Timeline

JSR 303: Bean Validation 1.0

  • Released: 2009

  • Purpose: Introduced the foundational API for validation using annotations.

  • Main Features:

    • Field-level constraint annotations

    • Custom constraint support

    • Integration with JPA and POJOs

  • Examples:

    public class User {
        @NotNull
        @Size(min = 5, max = 15)
        private String username;
    }

JSR 349: Bean Validation 1.1

  • Released: 2013

  • Enhancements:

    • Method and constructor validation

    • Cross-parameter constraint validation

    • Support for dependency injection containers (CDI, Spring)

  • Example:

    public class UserService {
        public void registerUser(@NotNull User user) {
            // Automatically validated
        }
    }

JSR 380: Bean Validation 2.0

  • Released: 2017

  • Target Platform: Java SE 8+

  • Major Improvements:

    • Support for Java 8 features:

      • Optional, java.time types

      • Repeatable annotations

      • Type use annotations (container element validation)

    • New built-in constraints:

      • @NotEmpty, @NotBlank, @Positive, @Negative, etc.

  • Example (with container validation):

    public class ShoppingCart {
        @NotEmpty
        private List<@NotNull @Size(min = 2) String> items;
    }

Summary Table of Bean Validation JSRs

JSR
Version
Released
Key Enhancements

303

1.0

2009

Basic annotation validation on fields

349

1.1

2013

Method/constructor validation, CDI

380

2.0

2017

Java 8 support, container validation, more annotations

Last updated

Was this helpful?