2017-11-01 2 views
1

컬렉션에 나열되거나 개별적으로 가져올 때 ResourceProcessor를 사용하여 내 리소스 개체에 추가 링크를 추가하고 있습니다. 그러나 저장소에 프로젝션 (또는 발췌 프로젝트)을 적용하면 ResourceProcessor가 실행되지 않으므로 해당 리소스에 대한 링크가 만들어지지 않습니다. 리소스 컨텐트가 어떻게 투사되는지에 관계없이 리소스에 내 사용자 지정 리소스 링크를 추가 할 수있는 방법이 있습니까?봄 데이터 나머지 ResourceProcessor가 프로젝션에 적용되지 않았습니다.

답변

1

나는이 문제가 귀하의 케이스를 설명 생각 : https://jira.spring.io/browse/DATAREST-713

현재 스프링 데이터 나머지는 문제를 해결하는 기능을 제공하지 않습니다.

우리는 여전히 각 돌출부에 대해 별도의 ResourceProcessor을 필요로 약간의 해결 방법을 사용하지만 우리는 링크 논리를 복제 할 필요가 없습니다

우리는 프로젝션에 대한 기본 엔터티를 얻을 수있는 기본 클래스가를 엔티티의 ResourceProcessor을 호출하고 링크를 프로젝션에 적용합니다. 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); 
    } 
} 

구현 자체는 엔티티 클래스를 처리하고 우리의 ProjectionResourceProcessor에 전달 할 수있는 ResourceProcessor를 전달합니다. 링크 생성 논리를 포함하지 않습니다.

관련 문제