Reputation: 1399
I create REST backend for my Android app (sth similar to online shop). I decided to use Spring-Boot because this framework is easy to configure and deploy. At this moment I have entities and repositories (CrudRepository). I know that I will need also controllers or something similar. I found few examples on Github (like this - https://github.com/steve-perkins/fitnessjiffy-spring) but in these examples are often used furthermore DTOs and Services.
What do you think, if these components (DTOs and Services) are necessary? If so, why? If DTOs is a good way, maybe you know a good trick to convert Entity to DTO to avoid code duplication? What is the difference between controllers and services in Spring?
Maybe you know any other frameworks in which I could do it well and faster? What about Jersey?
I would be grateful for any examples / links.
Upvotes: 3
Views: 665
Reputation: 1078
I have only used Jersey for web services but the principles should remain the same. I don't believe it is a technical requirement to have either DTOs or Services as defined in the Github sample but it is a good practice.
In the Jersey REST code that I developed my controllers and services were the same class. I did have DTOs because not all DTOs were an exact replica of my Entities. To map entities (fetched by the DAOs from the database) to DTOs I used Dozer. In some cases when DTOs would combine several attributes from multiple entities I would code the mapping.
For difference between Controllers and Services see this post.
You asked for "maybe you know a good trick to convert DAO to DTO to avoid code duplication". I assume you mean Entity to DTO.
Upvotes: 1