2

I'm trying to do pagination. What I have works, but I have some doubts about the way I get a Resource URL.

In my Resource Repository, I inject a ResourceRegistry in a Lazy way (I know it's a circular dependency) to get a URL for my Resource that I then use to generate my links.

  String resourceUrl = resourceRegistry.getResourceUrl(Book.class);

It works fine, but the circular dependency bothers me. Is there some static class to get a resource URL. Or perhaps there's a completely different way of approaching this?

user2793390
  • 741
  • 7
  • 29
  • Is there a link that you miss when katharsis returns the json? You should not need to create any link yourself. Have you seen `@JsonApiToOne` and it's siblings on [Katharsis docs](http://katharsis.io/docs)? – sorrymissjackson May 19 '16 at 16:16
  • @sorrymissjackson I'm talking about top-level 'links' attribute which I want to use for pagination - see http://jsonapi.org/. – user2793390 May 19 '16 at 16:21
  • I see. Have you tried `LinksRepository` interface to provide top-level `LinksInformation`? Though the interface doesn't seem to help you retrieve the urls. Looking quickly at katharsis' serializers I didn't spot another way of getting the URL (other than `resourceRegistry.getResourceUrl`). – sorrymissjackson May 20 '16 at 07:47
  • How did you get access to the `ResourceRegistry`? – sorrymissjackson May 20 '16 at 07:54
  • @sorrymissjackson I have a service class where I Autowire ResourceRegistry. That service class is then autowired in the ResourceRepository. ResourceRegistry needs to be autowired in a Lazy way cause it's a circular dependency. – user2793390 May 20 '16 at 14:06

1 Answers1

0

Maybe this is not a satisfying answer, but katharsis does generate links in it's serializers. You could probably extend one of the katharsis' serializers and make it aware of optional meta information, containing the total number of items available, which the client would need to know anyway. From this data, you could create the top level links for prev and next pages. In the katharsis serializers the resourceRegistry will be available anyway, along with QueryParams, containing the original pagination parameters. This is all very hypothetical though.

The suggested solution by katharsis devs would be:

@JsonApiFindAll
public JsonApiResponse findAll() {
    return new JsonApiResponse()
        .setEntity(Collections.singletonList(new Task(1L, "John")))
        .setLinksInformation(new LinksInformation() {
            public String self = "...";
            public String first = "...";
            public String prev = "...";
            public String next = "...";
            public String last = "...";
        });
}

Taken from here:

Still, hand-crafting the links seems required.

sorrymissjackson
  • 2,395
  • 1
  • 19
  • 18