Hello everyone,
I’m new to Spring Boot and REST APIs, and I’ve built a basic CRUD REST project to understand core concepts like controllers, services, repositories, DTOs, and entity relationships.
While developing this project, I made a design decision that I’m now unsure about and would really appreciate some validation or guidance from experienced developers.
My project link: chesszero-23/basicCRUDapplication
What I did
In my request and response DTOs, I directly used JPA entities instead of primitive IDs.
For example:
instead of:
Because of this, when I query my API using Postman, I get deeply nested responses like this:
[
{
"numberOfEmployees": 2345,
"employees": [
{
"firstName": "john",
"id": 1,
"lastName": "doe",
"salary": 20000
},
{
"firstName": "charlie",
"id": 2,
"lastName": "kirk",
"salary": 25000
}
],
"company": {
"branches": [
{
"branchId": 1,
"employees": [ ... ],
"numberOfEmployees": 2345
}
],
"companyId": 1,
"employees": [ ... ],
"name": "Amazon",
"numberOfEmployees": 2345,
"revenue": 24567
}
}
]
This is not an infinite loop, but the data is repeated and deeply nested, which doesn’t feel like good API design.
What I learned
After some discussion (and ChatGPT help), I learned that:
DTOs should not contain entities
DTOs should ideally contain primitive values or other DTOs
Relationships should be handled in the service layer, not the mapper
So now I’m trying to redesign my DTOs like this:
Example service logic I’m using now:
u/Service
public BranchCompleteDTO createBranch(BranchCreateDTO dto) {
Company company = companyRepository.findById(dto.companyId())
.orElseThrow(() -> new RuntimeException("Company not found"));
Branch branch = branchMapper.toBranch(dto);
branch.setCompany(company);
Branch saved = branchRepository.save(branch);
return toBranchCompleteDTO(saved);
}
My confusion
This approach feels much more verbose compared to directly using entities in DTOs.
For read APIs (like “get all branches”), if I want to show company name, I end up creating:
CompanySummaryDTO
EmployeeSummaryDTO
BranchCompleteDTO
This makes even a simple CRUD project feel over-engineered.
My questions
Is this DTO-heavy approach actually the correct and recommended way, even for small projects?
Is there a simpler or cleaner pattern for basic CRUD APIs that still follows good practices?
At what point does it make sense to use:
If possible, could you share a simple but well-structured CRUD Spring Boot project that I can refer to?
Goal
I’m not trying to over-optimize — I just want to:
learn correct habits early
understand why certain patterns are preferred
avoid building bad practices into my foundation
I have structured my question with ChatGPT help, Thanks for your answers.