如何使用Spring Data REST创建自定义PersistentEntityResourceAssembler

时间:2019-06-06 10:29:54

标签: java spring spring-data-rest spring-hateoas

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

我正在寻找一种过滤REST调用中字段的方法。我将使用https://github.com/bohnman/squiggly-java,但我想将其集成到Spring资源汇编器中。

我正在寻找一种自定义REST控制器中自动提供的PersistentEntityResourceAssembler的方法。 这是该类的代码:

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的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

您必须创建一个PersistentEntityResourceAssemblerArgumentResolver来创建 PersistentEntityResourceAssembler随时可以使用控制器方法。

它如此工作,因为PersistentEntityResourceAssembler需要请求中的projection参数,因此它不能是bean

自变量解析器本身已注册在RepositoryRestMvcConfiguration类中。 它是在其受保护的方法defaultMethodArgumentResolvers()中定义的。 不幸的是,无法通过RepositoryRestConfigurer对其进行配置,因此您需要扩展RepositoryRestMvcConfiguration配置类本身,然后覆盖defaultMethodArgumentResolvers()方法。

不幸的是,此方法还会创建很多其他argumentsResolvers,因此,我认为最好的方法是调用super方法,从返回列表中删除原始的PersistentEntityResourceAssemblerArgumentResolver并将自定义变量添加到其中。

这不是一件容易的事……祝你好运!