Java Bean Validation (commonly known as JSR 380 for Bean Validation 2.0) is not part of the Java SE core platform itself. Instead, it is a Java specification that defines a standard way to perform validation on Java beans.
Key points:
- Java Bean Validation API is defined by the JSR 380 (Bean Validation 2.0) specification.
- It is part of the Jakarta EE (formerly Java EE) ecosystem, but not included in the Java SE standard library.
- To use Bean Validation in a plain Java SE application, you need to add an implementation dependency, such as:
- Hibernate Validator (the reference implementation)
- Or other implementations that comply with the Bean Validation spec.
What does this mean practically?
- If you write a Java SE application and want to use Bean Validation annotations like
@NotNull, @Size, @Email, etc., you must add the Bean Validation API and an implementation to your project dependencies (e.g., via Maven or Gradle).
- The API provides annotations and interfaces.
- The implementation provides the runtime engine that performs the validation.
Example Maven dependencies to use Bean Validation in a Java SE project:
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version> <!-- or latest -->
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.5.Final</version> <!-- or latest -->
</dependency>
Summary
| Aspect | Java SE Core | Bean Validation (JSR 380) |
|---|
| Included in Java SE? | No | No |
| Part of Jakarta EE? | No | Yes |
| Requires external libs? | No | Yes |
| Provides annotations? | No | Yes |
| Provides runtime engine? | No | Yes (via implementation) |
If you want to do validation without adding external dependencies, you have to implement your own validation logic manually (as in your first question).
If you want to use standardized validation annotations and infrastructure, you need to add Bean Validation API and an implementation to your project.
Let me know if you want a quick example of how to use Bean Validation in a plain Java project!