How to add HATEOAS links in a sub resource

喜欢而已 提交于 2019-12-02 03:08:07
oferrero

Just stumbled on this question, even though it is quite old, but may be worth answering for others implementing similar functionality. Basically, you create embedded resources for AdminModuleResource on your AdminResource and build the links for these embedded resources within your AdminResourceAssembler. The code below is a simplified version of what is posted on this answer

On AdminResource add:

@JsonUnwrapped
private Resources<EmbeddedWrapper> embeddeds;
// + setters/getters

On AdminResourceAssembler add:

EmbeddedWrappers wrapper = new EmbeddedWrappers(true);

List<EmbeddedWrapper> wrappers = (List<EmbeddedWrapper>) super.buildEmbeddables(entity);
Set<AdminModuleResource> moduleResources = adminResource.getModuleResources( );
if(!moduleResources.isEmpty( ))
    wrappers.add(wrapper.wrap(adminModuleResourceAssembler.toResources(moduleResources)));

adminResource.setEmbeddeds(new Resources<>(wrappers));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!