I am sort of beginner in SDR and Spring HATEOAS tools..
Spring Data REST takes the features of Spring HATEOAS and Spring Data Neo4j and combines them together automatically.
My repository is:
@RepositoryRestResource(collectionResourceRel = "entity", path = "entity")
public interface MyRepository extends GraphRepository<EntityBean>, RelationshipOperationsRepository<EntityBean>, PagingAndSortingRepository<EntityBean, Long>{}
I can see there are two class available in HATEOAS to build the Links such as:
ControllerLinkBuilder
and JaxRsLinkBuilder
If we go through most of tuts out there even Spring Getting Started guide, it showing example for Controller. As far as I can guess both stood for two different things as per name i.e. Creating links with Controller and JAX-RS services respectively. However when I am trying to build the links in my REST Service:
EntityBean bean = myService.save(bean);
Resource<EntityBean> obj = new Resource<EntityBean>(bean);
obj.add(ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(TestResource.class).dummy("dummy")).withRel("entity"));
through either of them (i.e. ControllerLinkBuilder
or JaxRsLinkBuilder
) it always throwing Exception:
java.lang.IllegalStateException: Could not find current request via RequestContextHolder
But when I am trying to add them manually:
Link link = new Link("http://localhost:9090/amg-web/sdr/);
obj.add(link);
Where /amg-web/sdr/
is Servlet Mapping to RepositoryRestDispatcherServlet
and It working without any failure... with following output as application/hal+json
response type:
{
_links: {
entity: {
href: "http://localhost:9090/amg-web/sdr/entity{?page,size,sort}"templated: true
}-
}-
}
So there must be something wrong at **LinkBuilder..
My Question is:
Thanks !!
Ok, I think upto some extent I've found the answer for my questions:
Regarding java.lang.IllegalStateException
I was missing following Listner in my web.xml:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
GraphRepository
or PagingAndSortingRepository
etc. As far as I can get, Spring HATEOAS is Link building machine which use our REST service or Controller class to build links to given resource. for e.g.
Entity obj = repository.findByName(name);
Resource<Entity> resource = new Resource<Entity>(obj);
resource.add(JaxRsLinkBuilder.linkTo(TestResource.class).withRel("entity"));
return resource;
I think this is the only way to manually create links and relationship for every required entity beans, in case of SDR this was automatically controlled.
For validation or security, I found this link useful. but still trying to implement it.
I hope this helps to someone.. if possible please feel free to update or correct my answer. Thanks