I am trying to create a REST api using Spring MVC. Instead of writing CRUD methods for each of the controller, I created an abstract class that would take care of the typical REST CRUD scenarios. So I created these following classes
public interface DomainObject<ID extends Serializable> {}
public class Apps implements DomainObject<String>{}
public interface AppsRepository extends PagingAndSortingRepository<Apps, String> {}
public abstract class AbstractController<T extends PagingAndSortingRepository<DomainObject<Serializable>, Serializable>> {}
With the above class/interface definitions, how do I declare a class that extends the AbstractController?
I tried declaring AppsController using
public class AppsController extends AbstractController<AppsRepository>{}
without any luck. Eclipse is showing the following error
Bound mismatch: The type AppsRepository is not a valid substitute for the bounded parameter <T extends PagingAndSortingRepository<DomainObject<Serializable>,Serializable>> of the type AbstractController<T>
I am totally stuck. I have tried different approches for a while without any luck. Any help would be greatly appreciated.
In the same way as
List<String>is not a subtype ofList<Object>,PagingAndSortingRepository<Apps, String>(and thereforeAppsRepository) is not a subtype ofPagingAndSortingRepository<DomainObject<Serializable>, Serializable>You could use wildcards, as it is an extension of
PagingAndSortingRepository<? extends DomainObject<? extends Serializable>,? extends Serializable>>but if you do that then you’re very limited in what you can do in theAbstractController, for example you can’t have any methods that take parameters of the domain object’s ID type because you don’t know what that type is in the general case.You need to declare the relevant type variables in
AbstractController:and make
AppsControllerextendAbstractController<String, String, Apps, AppsRepository>.