Menu

1 / 4

Why I Stopped Exposing JPA Entities Directly in My Spring Boot API

DEV Community: java·KUMAR NISHANT·7 days ago
#8act8mrL
#devtojava#devto#dev#student#entity
Reading 0:00
15s threshold

When I started building my Student CRUD API using Spring Boot, my main focus was getting the basic operations working.

Creating a student, fetching students, updating student details, and deleting a student. I had already organized my application using the Controller → Service → Repository architecture.

But there was something I hadn't thought much about:

Should I use my JPA entity directly as the request and response object of my REST API?

Initially, I did exactly that.

It worked. I could send JSON from Postman, receive data from the API, and perform database operations without any problem. However, as I started improving the project, I realized that my database entity and my API's data structure shouldn't necessarily be the same thing.

That's when I introduced DTOs .

In this article, I'll explain what DTOs are, why I decided to use them, and how I implemented them in my Student CRUD API.

1. How I Initially Handled API Requests

Let's start with the approach I used before introducing DTOs.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.