First, I read the previous question: Disclosing a reference to a collection object in spring REST data
But the problem still persists without a trick.
Indeed, if I want to open a link for collection resources, I use the following code:
@Component public class FooProcessor implements ResourceProcessor<PagedResources<Resource<Foo>>> { private final FooLinks fooLinks; @Inject public FooProcessor(FooLinks fooLinks) { this.FooLinks = fooLinks; } @Override public PagedResources<Resource<Foo>> process(PagedResources<Resource<Foo>> resource) { resource.add(fooLinks.getMyCustomLink()); return resource; } }
This works correctly, unless the collection is empty ...
The only way to work is to replace my following code with:
@Component public class FooProcessor implements ResourceProcessor<PagedResources> { private final FooLinks fooLinks; @Inject public FooProcessor(FooLinks fooLinks) { this.FooLinks = fooLinks; } @Override public PagedResources process(PagedResources resource) { resource.add(fooLinks.getMyCustomLink()); return resource; } }
But at the same time, the link will be displayed for all collections.
I can create a condition for setting only what I want, but I do not think that it is clean.
java spring spring-data-rest spring-hateoas
Kakawait
source share