簡體   English   中英

如何使用Spring Data REST創建自定義PersistentEntityResourceAssembler

[英]How to create custom PersistentEntityResourceAssembler with Spring Data REST

我正在使用Spring Boot 2.1,Spring Data REST,Spring HATEOAS,Hibernate 5。

我正在尋找一種過濾REST調用中的字段的方法。 我將使用https://github.com/bohnman/squiggly-java,但我想將其集成到Spring資源匯編器中。

我正在尋找一種自定義PersistentEntityResourceAssembler的方法,該方法可在REST控制器中自動使用。 這是該類的代碼:

public class PersistentEntityResourceAssembler implements ResourceAssembler<Object, PersistentEntityResource> {
    @NonNull
    private final PersistentEntities entities;
    @NonNull
    private final Projector projector;
    @NonNull
    private final Associations associations;
    @NonNull
    private final SelfLinkProvider linkProvider;
    @NonNull
    private final EmbeddedWrappers wrappers = new EmbeddedWrappers(false);

    public PersistentEntityResource toResource(Object instance) {
        Assert.notNull(instance, "Entity instance must not be null!");
        return this.wrap(this.projector.projectExcerpt(instance), instance).build();
    }

    public PersistentEntityResource toFullResource(Object instance) {
        Assert.notNull(instance, "Entity instance must not be null!");
        return this.wrap(this.projector.project(instance), instance).build();
    }

    private Builder wrap(Object instance, Object source) {
        PersistentEntity<?, ?> entity = this.entities.getRequiredPersistentEntity(source.getClass());
        return PersistentEntityResource.build(instance, entity).withEmbedded(this.getEmbeddedResources(source)).withLink(this.getSelfLinkFor(source)).withLink(this.linkProvider.createSelfLinkFor(source));
    }

    private Iterable<EmbeddedWrapper> getEmbeddedResources(Object instance) {
        return (new EmbeddedResourcesAssembler(this.entities, this.associations, this.projector)).getEmbeddedResources(instance);
    }

    public Link getSelfLinkFor(Object instance) {
        Link link = this.linkProvider.createSelfLinkFor(instance);
        return new Link(link.expand(new Object[0]).getHref(), "self");
    }

    public PersistentEntityResourceAssembler(@NonNull PersistentEntities entities, @NonNull Projector projector, @NonNull Associations associations, @NonNull SelfLinkProvider linkProvider) {
        if (entities == null) {
            throw new IllegalArgumentException("entities is marked @NonNull but is null");
        } else if (projector == null) {
            throw new IllegalArgumentException("projector is marked @NonNull but is null");
        } else if (associations == null) {
            throw new IllegalArgumentException("associations is marked @NonNull but is null");
        } else if (linkProvider == null) {
            throw new IllegalArgumentException("linkProvider is marked @NonNull but is null");
        } else {
            this.entities = entities;
            this.projector = projector;
            this.associations = associations;
            this.linkProvider = linkProvider;
        }
    }
}

我的猜測是它注入了Spring的某個地方。 我想自定義JSON內容,以動態過濾掉不需要的字段。 我試圖創建該類的副本並使用@Component對其進行批注,但缺少投影儀。

在Spring Data REST中自定義PersistentEntityResourceAssembler的正確方法是什么?

您必須創建一個PersistentEntityResourceAssemblerArgumentResolver ,如果控制器方法需要它,則可以動態創建PersistentEntityResourceAssembler

之所以這樣工作,是因為PersistentEntityResourceAssembler需要請求中的projection參數,因此它不能是bean

參數解析器本身在RepositoryRestMvcConfiguration類中注冊。 它是在其受保護的方法中定義的: defaultMethodArgumentResolvers() 不幸的是,無法通過RepositoryRestConfigurer對其進行配置,因此您需要擴展RepositoryRestMvcConfiguration配置類本身,然后覆蓋defaultMethodArgumentResolvers()方法。

不幸的是,該方法也會創建很多其他argumentsResolvers,因此我認為最好的方法是調用super方法,從返回列表中刪除原始的PersistentEntityResourceAssemblerArgumentResolver並將自定義對象添加到其中。

這將不是一件容易的事……祝您好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM