Monday, 21 July 2025

Difference between jakarta.validation-api & spring-boot-starter-validation

 

🔹 1. jakarta.validation-api

This provides the standard interface (API) for bean validation.
It includes annotations like:


@NotNull @Size(min = 3, max = 10) @Email @Min(1) @Max(100)

Dependency:


<dependency> <groupId>jakarta.validation</groupId> <artifactId>jakarta.validation-api</artifactId> </dependency>

🔸 But: this is only the API, not the implementation.
You still need something to run these validations (see below).


🔹 2. spring-boot-starter-validation

This is a Spring Boot starter that:

  • Automatically includes the jakarta.validation-api

  • Adds the actual implementation (Hibernate Validator)

  • Integrates validation into Spring Boot (like @Valid in controllers)

Dependency:


<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>

✅ So Which One Should You Use?

You should only use:


<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>

Because:

  • It already includes jakarta.validation-api

  • It also brings in Hibernate Validator

  • Works perfectly with Spring Boot MVC / REST with @Valid


✨ Example in Spring Boot:


public class UserDTO { @NotNull @Size(min = 3, max = 20) private String name; @Email private String email; }

In controller:


@PostMapping("/user") public ResponseEntity<String> createUser
(@Valid @RequestBody UserDTO userDto) { return ResponseEntity.ok("Valid user!"); }


-----------------Difference----------


🎯 Use in Real Projects

In Spring Boot apps, always use:


<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>

It brings:

  • Jakarta API (@NotNull, etc.)

  • Hibernate Validator engine

  • Spring integration (works with @Valid in controllers)


🧠 Tricky Interview Q&A

1. Q: If I only add jakarta.validation-api to my pom.xml, will validation work?

👉 A: No, because jakarta.validation-api only provides interfaces and annotations.

You still need an implementation (like Hibernate Validator).


2. Q: What's the benefit of using spring-boot-starter-validation over directly

adding jakarta.validation-api and Hibernate Validator?

👉 A: spring-boot-starter-validation bundles everything you need and

auto-configures it for Spring Boot. Less config, more productivity.


3. Q: Can I use Jakarta Validation in non-Spring Java applications?

👉 A: Yes, but you must manually include both jakarta.validation-api and

an implementation like Hibernate Validator.


4. Q: What happens behind the scenes when I annotate a DTO with @Valid

in Spring Boot?

👉 A: Spring uses the Hibernate Validator (from the starter) to validate the

fields based on annotations. If validation fails,

a MethodArgumentNotValidException is thrown and handled globally.


5. Q: Which validation provider is included by default in Spring Boot

Starter Validation?

👉 A: Hibernate Validator.


Happy Learning... :)


No comments:

Post a Comment