4
I have read many articles and seen many lessons on Java and lately I come across a lot with the concept of services and controllers, I have the clarity of the role of the Controller within an MVC structure, but I have some questions about the role of the service within a project:
What would be the role of the service exactly ?
What exactly differs a service from a controller ?
Where did this service concept come from ? ( Part of some new architecture ? )
Do you have any example of a project that clearly shows the difference between one and the other or any study material that explains this in more detail ?
I found some answers in the global Stack Overflow, but nothing that made that clear to me.
Briefly, the controller is responsible for receiving an HTTP request and generating an HTTP response... and only. All the logic of the application must be in another structure, usually in the service. This stereotype runs away from the standard that the controller calls the model. Here the controller calls the service and the service that calls the model and thus you are free to perform the same action from different sources that are not HTTP requests, such as a CLI call, for example.
– Woss
I think I’m beginning to understand the concept, is that I’ve always used Resources to expose my endpoints and it passed the generated objects to the controller and that did its job, so different forms of HTTP access, gRPC among others all had a specific package that took care of the serialization of the data to be sent to the controller.
– viniciusxyz
In this case who is responsible for all the flow that was previously the controller is the service and the controller’s work is like something more generic like the Resources package that I mentioned ?
– viniciusxyz