2016-12-09 5 views
1

誰も助けてください。春のデータ休憩。リクエストURIを通じてリポジトリクラスを取る方法

私はいくつかのエンティティとリポジトリを私のアプリケーションの中に持っています。これは春のデータ休憩に基づいています。 今、私は自分のHandlerInterceptorの実装を書いて各リクエストのプリハンドリングを行いました。どのHTTPリクエストにどのリポジトリが使用されるのかを知る必要があります。

私はリポジトリごとに別々のインターセプタを作成できますが、このソリューションは柔軟性がありません。事前

答えて

1

リポジトリ用

ThxをはRepositoriesクラスを使用して、スプリングデータ休止で解決されています。

repositoriesクラスは、与えられたクラスのリポジトリを見つけるヘルパーメソッド(getRepositoryFor(Class<?>))を公開します。

あなたのインターセプタで指定されたクラスのリポジトリを見つけるための次のスニペットを使用することができます。

Repositories repositories = new Repositories(appContext); 
repositories.getRepositoryFor(entityObject.getClass()); 

よりエレガントなソリューションが内蔵されたバネのデータレストリポジトリのルックアップを利用することであろうカスタムコントローラー(RootResourceInformationHandlerMethodArgumentResolver)を使用した実装

このためには、RepositoryRestControllerエンドポイントメソッドにRootResourceInformationパラメーターを追加するだけです。

 @RepositoryRestController 
     @RequestMapping("/customName") 
     public class RepositoryExportController { 

     @Autowired 
     private ApplicationContext appContext; 

     @RequestMapping(method = RequestMethod.GET, value = "{repository}", 
        produces = MediaTypes.HAL_JSON_VALUE) 
     @ResponseBody 
      public Resources<Resource<?>> export(RootResourceInformation resourceInformation, ...) { 

     Repositories repositories = new Repositories(appContext); 

     CrudRepository repo=(CrudRepository)repositories.getRepositoryFor(resourceInformation.getDomainType()); 

      repo.findAll(); 
     ...other logic.... 
    } 
+0

これにより、私はspring-security-aclのための適切なコントローラメソッドをサブリソースとして実装することができました。 – mbonato

関連する問題