Issue
I have the following problem while creating a model in Spring Boot.
Created an abstract model class -
public abstract class AbstractRequest {
private String tariffType;
}
And his heir
public class Request extends AbstractRequest {
// some fields
}
In the controller I pass an abstract request
@PostMapping
public ResponseEntity<PriceResponse> calculate(@RequestBody AbstractRequest request) {
return ResponseEntity.ok(calculationService.calculate(request));
}
The service interface also has an abstract request class
public interface CalculationService {
PriceResponse calculate(AbstractRequest request);
}
In the implementation of the service interface, I am trying to accept the successor of the abstract model
@Service
public class CalculationServiceFTL implements CalculationService {
@Override
public PriceResponse calculate(Request request) {
}
}
And as a result I get an error
Class 'CalculationServiceFTL' must either be declared abstract or implement abstract method 'calculate(AbstractRequest)' in 'CalculationService'
Please explain why spring does not accept an abstract class heir as an argument and how should I be in this case if I can have several abstract model heirs and I need to process them differently in different services. Thanks
Solution
An abstract class cannot be instantiated directly but by its inheriting concrete classes.
Any concrete class inheriting the abstract class should be instantiable with a constructor (if not abstract itself of course). But use of a constructor is prohibited by Spring because any class that we instantiate this way cannot be taken in charge by Spring’s automated dependency injection.
Answered By - Sandeep Kumar Nat
Answer Checked By - Willingham (JavaFixing Volunteer)