Spring Data Rest integration with Spring HATEOAS
Asked Answered
S

1

2

From various documents and tuts, I've earned and learned following points so far:

  1. Spring Data Rest (SDR) used for exposing our Spring Data Repository as REST service, so that one can use it for self exploring, without any need of creating JAXRS manually. It only works for Repository layer, and we cannot control its way of working in terms of modification or addition other than the configuration using RepositoryRestMvcConfiguration. It uses Spring HATEOAS internally somewhere.

  2. Spring HATEOAS is made for creating links within Entities we return through Controller or REST endpoints. We got ResourceSupport to extend our entity or Resource wrapper class to wrap our Entity to create or add links. There are several Annotations and classes to use such as @EnableHyperediaSupport and EntityLinks.

There may be some points which I am yet to explore or get to know about, but I was just curious about How can we combine SDR into HATEOAS link building process ? Say for eg.

EntityBean bean = repository.findByName(name);
Resource<EntityBean> resource = new Resource<EntityBean>(bean);
//JaxRsLinkBuilder.linkTo(TestResource.class).withRel("entity")     // this also works   
//ControllerLinkBuilder.linkTo(TestResource.class).withRel("myRel") // this also works
// I am curious how ControllerLinkBuilder and JaxRSLinkBuilder both are working for JaxRS.  
//Here TestResource is my REST service class. now in below line: 
resource.add(JaxRsLinkBuilder.linkTo(MyRepository.class).withRel("sdf")); //not working
// MyRepository is SDR exposed repository, which I thought should work but not working.
return resource;  

So, I just wanted to include my exposed REST repository into manual HATEOAS link building process.. is it possible to do so ?

Splanchnic answered 16/5, 2014 at 14:16 Comment(0)
R
3

You should able to use Spring-HATEOAS ResourceProcessor to build links.

Example:

@Component
public class MyBeanResourceProcessor implements ResourceProcessor<Resource<MyBean>> {

    @Autowired
    private EntityLinks entityLinks;

    public Resource<MyBean> process(Resource<MyBean> resource) {
        MyBean mybean = resource.getContent();

        // Do your linking here using entity class
        //EntityBean bean = repository.findByName(name);
        //Resource<EntityBean> resource = new Resource<EntityBean>(bean);
        // assuming you are linking to a single resource and bean.getId() method... check entitylinks for other methods
        //resource.add(entityLinks.linkForSingleResource(bean.class,bean.getId()).withRel("sdf"));

        return resource;
    }

}
Ruyter answered 21/5, 2014 at 14:34 Comment(1)
To clarify the solution here which is correct. You want to do the opposite of what you are asking. That's is you don't include spring-data-rest into hateos, rather spring-data-rest includes hateos already. So use spring-data-rest and then define a custom hateos resource-processor which spring-data-rest will use.Sighted

© 2022 - 2024 — McMap. All rights reserved.