2017-11-01 12 views
1

私は、ResourceProcessorを使用して、コレクションにリストされている、または個別にフェッチされたときに、リソースオブジェクトへのリンクを追加しています。しかし、リポジトリに投影(または抜粋プロジェクト)を適用すると、ResourceProcessorが実行されず、そのリソースのリンクが作成されません。リソースの内容がどのように投影されるかに関係なく、カスタムリソースリンクをリソースに追加できるようにする方法はありますか?SpringデータRest ResourceProcessorがプロジェクションに適用されていません

答えて

1

私は、この問題はあなたのケースを記述していると思う: https://jira.spring.io/browse/DATAREST-713

現在、春・データ-残りはあなたの問題を解決するための機能を提供していません。

我々はまだ各投影用の別々のResourceProcessorを必要としますが、我々は、リンク・ロジックを複製する必要はありません少しの回避策を使用している:我々は、プロジェクション用の基礎となるエンティティを取得することができ、基本クラスを持っている

をエンティティのResourceProcessorを呼び出し、そのリンクをProjectionに適用します。 EntityはすべてのJPAエンティティの共通のインターフェイスですが、org.springframework.data.domain.Persistableまたはorg.springframework.hateoas.Identifiableも使用できると思います。

/** 
* Projections need their own resource processors in spring-data-rest. 
* To avoid code duplication the ProjectionResourceProcessor delegates the link creation to 
* the resource processor of the underlying entity. 
* @param <E> entity type the projection is associated with 
* @param <T> the resource type that this ResourceProcessor is for 
*/ 
public class ProjectionResourceProcessor<E extends Entity, T> implements ResourceProcessor<Resource<T>> { 

    private final ResourceProcessor<Resource<E>> entityResourceProcessor; 

    public ProjectionResourceProcessor(ResourceProcessor<Resource<E>> entityResourceProcessor) { 
     this.entityResourceProcessor = entityResourceProcessor; 

    } 

    @SuppressWarnings("unchecked") 
    @Override 
    public Resource<T> process(Resource<T> resource) { 
     if (resource.getContent() instanceof TargetAware) { 
      TargetAware targetAware = (TargetAware) resource.getContent(); 
      if (targetAware != null 
        && targetAware.getTarget() != null 
        && targetAware.getTarget() instanceof Entity) { 
       E target = (E) targetAware.getTarget(); 
       resource.add(entityResourceProcessor.process(new Resource<>(target)).getLinks()); 
      } 
     } 
     return resource; 
    } 

} 

なリソースプロセッサの実装は次のようになります。

@Component 
public class MyProjectionResourceProcessor extends ProjectionResourceProcessor<MyEntity, MyProjection> { 

    @Autowired 
    public MyProjectionResourceProcessor(EntityResourceProcessor resourceProcessor) { 
     super(resourceProcessor); 
    } 
} 

実装自体は単なるエンティティクラスを扱うことができるResourceProcessorを通過して、私たちのProjectionResourceProcessorに渡します。リンク作成ロジックは含まれていません。

関連する問題