How to add HATEOAS links in a sub resource

前端 未结 1 1856
一整个雨季
一整个雨季 2021-01-25 02:54

I have a parent resource called the AdminResource and a child resource called the AdminModuleResource.

The resource of the parent is correctly fitted with HATEOAS links:

相关标签:
1条回答
  • 2021-01-25 03:24

    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));
    
    0 讨论(0)
提交回复
热议问题