0

I'm trying to set up a system with multiple applications connecting by use of a discovery server. I can traverse the hal responses to a specific resource, but I'm looking for a solution to get from a collection resource to a single resource and find the data for a specific entity.

In 1 application I have a RepositoryRestResource exposing some object:

@RestRepositoryResource(collectionResourceRel="things", itemResourceRel="thing") public interface ThingRepo extends CrudRepository<Thing,Long> {}

In some other application, I would like to get to a single thing. I have the id (let's say it's 1) and have the relation name of the collection and the single resource.

I would like to use a DiscoveredResource to get a link to this single item resource, or to the collection resource which I can then somehow expand using the ID (which would require a templated resource). If at all possible I would not like to just add "/1" at the end of the URL.

this is how I currently create a DiscoveredResource to point to the collection resource:

new DiscoveredResource(new DynamicServiceInstanceProvider(discoveryClient, traverson -> traverson.follow("things"));

Should I and is it possible to add a templated link on a collection resource created by a @RepositoryRestResource. Or is there some other trick I am missing?

bugfreerammohan
  • 1,471
  • 1
  • 7
  • 22
p.streef
  • 3,652
  • 3
  • 26
  • 50

1 Answers1

0

The solution here is to add a custom method as a @RestResource which exposes a relation with a templates URL you can then follow to.

Repo:

@RestRepositoryResource(collectionResourceRel="things", itemResourceRel="thing") public interface ThingRepo extends CrudRepository<Thing,Long> {

    @RestResource(rel = "thing")
    Thing findOneById(@Param("id") Long id);
}

Discovery + traverson:

DiscoveredResource resource = new DiscoveredResource(new DynamicServiceInstanceProvider(discoveryClient, traverson -> traverson.follow("things","search","thing"));
Link link = resource.getLink().expand(id);
p.streef
  • 3,652
  • 3
  • 26
  • 50