我使用的是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对其进行注释,但它缺少Projector。
在Spring Data REST中定制PersistentEntityResourceAssembler的正确方法是什么?
发布于 2019-06-07 01:04:22
您必须创建一个PersistentEntityResourceAssemblerArgumentResolver,以便在控制器方法需要时动态创建PersistentEntityResourceAssembler。
它是这样工作的,因为PersistentEntityResourceAssembler需要请求中的projection参数,所以它不能是bean__。
参数解析器本身在RepositoryRestMvcConfiguration类中注册。它在其受保护的方法中定义:defaultMethodArgumentResolvers()。不幸的是,它不能通过RepositoryRestConfigurer进行配置,因此您需要扩展RepositoryRestMvcConfiguration配置类本身,然后覆盖defaultMethodArgumentResolvers()方法。
不幸的是,此方法还创建了许多其他argumentResolvers,因此我认为最好的方法是调用超级方法,从返回列表中删除原始PersistentEntityResourceAssemblerArgumentResolver,然后将您的自定义one添加到其中。
这不会是一件容易的事。祝好运!
https://stackoverflow.com/questions/56475716
复制相似问题