I\'m trying to do something I think should be really simple. I have a Question object, setup with spring-boot, spring-data-rest and spring-hateoas. All the basi
I believe I've solved this problem in a fairly straightforward way, although it could have been better documented.
After reading the implementation of SimplePagedResourceAssembler I realized a hybrid solution might work. The provided Resource> class renders entities correctly, but doesn't include links, so all you need to do is add them.
My QuestionResourceAssembler implementation looks like this:
@Component
public class QuestionResourceAssembler implements ResourceAssembler> {
@Autowired EntityLinks entityLinks;
@Override
public Resource toResource(Question question) {
Resource resource = new Resource(question);
final LinkBuilder lb =
entityLinks.linkForSingleResource(Question.class, question.getId());
resource.add(lb.withSelfRel());
resource.add(lb.slash("answers").withRel("answers"));
// other links
return resource;
}
}
Once that's done, in my controller I used Option 2 above:
return pagedResourcesAssembler.toResource(page, questionResourceAssembler);
This works well, and isn't too much code. The only hassle is you need to manually add links for each reference you need.