2 minutes
Shaping Jakarta Agentic AI Together – Watch the Open Conversation
Earlier this week, we hosted Jakarta Agentic AI, An Open Conversation, an open house Jakarta TechTalk session, exploring a […]
Modern web applications often adopt a layered architecture to separate concerns and improve maintainability. In this paradigm, your REST API acts as a communication layer, exposing resources to clients while interacting with the deeper layers of your application. Leveraging Java Records as Data Transfer Objects (DTOs) in this context helps to simplify the design of your REST resources, making them more expressive and easier to work with.
While it might seem tempting to directly expose your database entities (often modelled with Jakarta Persistence (JPA)) as REST resources, there are several compelling reasons to avoid this approach:
DTOs introduce a layer of separation between your database entities and the API layer. They are simple data containers tailored specifically to the data your API needs to send and receive.
Benefits of DTOs:
Java Records, first introduced in Java 14, are an excellent fit when defining DTOs. They offer:
Let’s imagine a Book entity in our database with additional relationships:
@Entity
public class Book {
@Id
private int id;
private String title;
private String author;
@ManyToOne
private Publisher publisher;
// ... other fields, relationships
}
Now, a simplified DTO using a Java Record for our REST layer would look like:
public record BookDTO(int id, String title, String authorName) {}
Notice how the BookDTO only includes the crucial information needed for most API interactions, hiding the relationship to the Publisher. Our REST endpoint can then consume and return a BookDTO as follows:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public BookDTO saveBook(@Valid BookDTO bookDTO) {
return persistenceService.saveBook(bookDTO);
}
@GET
@Path("{title}")
public List<BookDTO> filterBooks(@NotEmpty @PathParam("title") String title) {
return queryService.filterBooks(title);
}
Within your REST resource classes, you’d likely use a mapping library (like MapStruct) to convert between your entities and their corresponding DTOs, managing the data flow between the REST layer and your service/persistence layers in an automated and transparent way.
By using DTOs and leveraging the conciseness of Java Records, you can create REST APIs that are more secure, maintainable and expressive. This separation of concerns gives you the flexibility to evolve your database schema and your API contract independently. Happy Coding!
Share:
2 minutes
Earlier this week, we hosted Jakarta Agentic AI, An Open Conversation, an open house Jakarta TechTalk session, exploring a […]
5 minutes
Published a little later than usual due to a busy conference season, this edition looks back at the key […]
4 minutes
Spring Framework 7 and Spring Boot 4 officially arrived, marking a key milestone for the Java ecosystem. From improved startup performance and modularization to native-image […]
Very well to understand.