Pregunta

I am sort of beginner in SDR and Spring HATEOAS tools..

As per Spring Neo4j Rest Doc:

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:

  1. How to create links targeting to Controller or JAX-RS Service or even to Exposed Repository (using Spring Data REST or Spring HATEOAS) !! ?
  2. If there are more than one type of JSON data in fetch request and if there are lots of links are needed, do we need to create links on ad-hoc basis as above or is there a way to generate it automatically ?
  3. Is there any security concern to exposed Repository as REST service ?

Thanks !!

¿Fue útil?

Solución

Ok, I think upto some extent I've found the answer for my questions:

  1. 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>  
    
  2. SDR and Spring HATEOAS both are two different dancers, where SDR dancing with HATEOAS but HATEOAS can do a solo dance. SDR basically expose our Spring Data powered repository as REST service which can be used as self exploring machine as per our repository design and its extended other repository such as GraphRepository or PagingAndSortingRepository etc.
  3. 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.

  4. 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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top