2

SpringMVCを使用してRESTAPIを作成しようとしています。コントローラごとにCRUDメソッドを作成する代わりに、一般的なRESTCRUDシナリオを処理する抽象クラスを作成しました。だから私はこれらの次のクラスを作成しました

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>> {}

上記のクラス/インターフェイス定義で、AbstractControllerを拡張するクラスを宣言するにはどうすればよいですか?

を使用してAppsControllerを宣言してみました

public class AppsController extends AbstractController<AppsRepository>{}

運がない。Eclipseは次のエラーを示しています

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>

私は完全に立ち往生しています。私はしばらくの間、運がなくてもさまざまなアプローチを試してきました。どんな助けでも大歓迎です。

4

1 に答える 1

1

List<String>のサブタイプではないのと同じようにList<Object>PagingAndSortingRepository<Apps, String>(したがってAppsRepository)はのサブタイプではありませんPagingAndSortingRepository<DomainObject<Serializable>, Serializable>

の拡張であるため、ワイルドカードを使用できますが、PagingAndSortingRepository<? extends DomainObject<? extends Serializable>,? extends Serializable>>これを行うと、で実行できる操作が非常に制限されます。AbstractControllerたとえば、ドメインオブジェクトのIDタイプのパラメータを受け取るメソッドを使用することはできません。一般的な場合、そのタイプが何であるかわかりません。

AbstractController関連する型変数を:で宣言する必要があります。

public abstract class AbstractController<IDT extends Serializable, KT extends Serializable,
    Dom extends DomainObject<IDT>, T extends PagingAndSortingRepository<Dom, KT>> {

  private T repository;

  public AbstractController(T repository) {
    this.repository = repository;
  }

  public Dom get(IDT objectId) {
    return repository.get(objectId);
  }
}

AppsController拡張しAbstractController<String, String, Apps, AppsRepository>ます。

于 2012-11-14T09:03:04.310 に答える