In my experience this is how ive seen spring controllers used : Define a spring controller which returns a value of some type to presentation layer. The controller request mapping method calls a service layer. The service layer itself consists of an interface and implementation. The service interface always contains just one method so its not really polymorphic as it keeps 'one form' consistently. The service implementation may access data of some kind, perhaps from a DAO and returns it the controller. The controller may amend this data slightly before returning its returned to the presentation layer.
What is this point of having an interface in this case ? Ive never encountered a spring service implementation called from multiple controllers, so why the interface ?
Does it not make more sense to use a helper controller class which performs the actions of the service implementation ?
Using Service layer is beneficial because it allows good logical separation of business logic from controller tasks. In Service class you can encapsulate business logic related to certain aspect like PaymentService. In PaymentService you can implement various methods like cardPayment(), paypalPayment(), refund(). Different controllers will be using the one single service. Moreover, service layer is also convenient for code reuse.
Using interfaces is convenient if you decide later to use some AOP features, add some logic (logging for example), in between of controller and service without changing it's code.
You're right - a service interface tends to be use-case specific, however there are still a few good reasons to create an interface:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With