Jakarta Dependency
Jakarta dependencies in Spring Boot are modern Java APIs that Spring uses for common enterprise tasks like database access, web handling, and validation. They replaced the older javax.* APIs to keep up with the latest standards.
What is Jakarta in Spring Boot?
Jakarta is the new namespace for enterprise Java specifications (like persistence, servlets, validation).
Spring Boot 3+ uses these Jakarta APIs to build applications with standard enterprise features.
It ensures your app uses the latest Java enterprise standards and works well with modern servers.
Use in Spring Boot / Modern Java
Even though Spring Boot is widely used, Jakarta EE APIs are still used under the hood, for example:
jakarta.persistence.Entity→ for JPA entitiesjakarta.validation.constraints.NotNull→ for bean validationjakarta.transaction.Transactional→ for managing transactions
Basically, Spring Boot leverages Jakarta EE standards so your app is compliant and portable, while giving you extra features.
1. Jakarta Persistence (JPA)
Used for database mapping and ORM.
Maven dependency:
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.1.0</version>
</dependency>
2. Jakarta Validation
Used for bean validation (@NotNull, @Size, etc.)
Maven dependency:
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.1.2</version>
</dependency>
3. Jakarta Transactions
Used for managing database transactions
<dependency>
<groupId>jakarta.transaction</groupId>
<artifactId>jakarta.transaction-api</artifactId>
<version>2.0.1</version>
</dependency>
4. Jakarta Servlets / Web
Used for web APIs, servlets, and filters
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
In Spring Boot
Most of the time, you don’t need to explicitly add all Jakarta dependencies, because Spring Boot starter dependencies already include the relevant Jakarta APIs under the hood. For example:
spring-boot-starter-data-jpa→ includesjakarta.persistencespring-boot-starter-web→ includesjakarta.servletand related web APIsspring-boot-starter-validation→ includesjakarta.validation
1️⃣ Jakarta Persistence (JPA)
Used to map Java objects to database tables.
Entity Example:
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// getters and setters
}
Repository Example:
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {}
Purpose: Automatically handles database mapping, queries, and CRUD operations.
2️⃣ Jakarta Validation
Used to validate fields in objects (e.g., incoming API requests).
DTO Example:
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Size;
public class UserDTO {
@NotNull(message = "Name cannot be null")
@Size(min = 2, max = 50)
private String name;
@Email(message = "Email should be valid")
private String email;
// getters and setters
}
Controller Example:
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping
public String createUser(@RequestBody @Valid UserDTO userDTO) {
return "User is valid: " + userDTO.getName();
}
}
Purpose: Ensures API data integrity before processing.
3️⃣ Jakarta Transactions
Used to manage database transactions.
Example Service:
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository repo) {
this.userRepository = repo;
}
@Transactional // ensures both operations succeed or fail together
public void updateUserAndLog(User user) {
userRepository.save(user);
// imagine logging to another table
// if any exception occurs, both operations roll back
}
}
Purpose: Guarantees atomic operations — either all succeed or none.
4️⃣ Jakarta Servlets / Web
Used to handle HTTP requests at a lower level (Spring uses it under the hood).
Servlet Example:
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().write("Hello from Jakarta Servlet!");
}
}
Purpose: The foundation for web requests/responses. Spring Boot automatically configures it via @RestController, so you rarely write raw servlets.
✅ Summary of Use-Cases:
| Jakarta Service | Purpose |
| Persistence (JPA) | Map objects to DB and simplify CRUD |
| Validation | Check request data correctness |
| Transactions | Manage atomic DB operations |
| Servlets / Web | Handle HTTP requests/responses |
1️⃣ What is a Servlet?
A Servlet is a Java class that handles HTTP requests and responses.
Think of it as the bridge between a web client (browser) and your Java application.
It lives on a web server (like Tomcat, Jetty).
Handles GET, POST, PUT, DELETE requests.
Generates a response, typically HTML, JSON, or plain text.
Basic Example:
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.getWriter().write("Hello from Servlet!");
}
}
HttpServletRequest→ contains data from client (query params, headers, body).HttpServletResponse→ allows sending data back to client.
“Spring Boot @RestControllers are built on top of Servlets.”
Here’s what that means:
When you write a
@RestController, you don’t see the servlet, but under the hood, Spring Boot runs aDispatcherServlet(a special Servlet).The
DispatcherServletreceives all incoming HTTP requests and routes them to your controller methods.So, every REST endpoint you define ultimately relies on the Servlet API to handle HTTP requests and responses.
💡 Analogy:
Think of Servlets as the “engine” of a car. @RestController is the dashboard and controls you interact with. The car runs because the engine (Servlet) is doing its job, even if you don’t touch it.