代码之家  ›  专栏  ›  技术社区  ›  LiamRyan

从REST资源集合中移除嵌入的

  •  0
  • LiamRyan  · 技术社区  · 8 年前

    _embedded /characters 在我的springboot应用程序中。

    _嵌入 不出席 characterDescriptions 既然它们是页面的主要焦点,有没有可能做到这一点?我应该尝试实现这个目标还是应该 _嵌入

    当我使用链接导航到特定资源时(比如 characters/1 例如)我应该链接回 /字符 父页还是只在这类端点包含自链接是可以接受的(我将最终链接到用户,但这是关于REST端点的一般问题)

    {
        "_embedded": {
            "characterDescriptions": [
                {
                    "characterName": "Adrak",
                    "playerName": "Liam",
                    "userName": "liam",
                    "_links": {
                        "self": {
                            "href": "http://localhost:8080/characters/1"
                        }
                    }
                },
                {
                    "characterName": "Thorny",
                    "playerName": "Aedo",
                    "userName": "aedo",
                    "_links": {
                        "self": {
                            "href": "http://localhost:8080/characters/2"
                        }
                    }
                },
                {
                    "characterName": "Anin",
                    "playerName": "Saoirse",
                    "userName": "saoirse",
                    "_links": {
                        "self": {
                            "href": "http://localhost:8080/characters/3"
                        }
                    }
                }
            ]
        },
        "_links": {
            "self": {
                "href": "http://localhost:8080/characters"
            }
        }
    }
    

    这是相关的方法

        @GetMapping
        public ResponseEntity<Resources<Resource<CharacterDescription>>> getAllCharacterDescriptions( ) {
    
            List <Resource<CharacterDescription>> characters = repository.findAll()
                    .stream().map( character -> {
                        Link characterLink = linkTo(methodOn(CharacterDescriptionController.class)
                                .getCharacterDescription(character.getCharacterId()))
                                .withSelfRel();
                        return new Resource<>(character, characterLink);
                    }).collect(Collectors.toList());
    
            Link allCharacterLink = linkTo(methodOn(CharacterDescriptionController.class)
                                            .getAllCharacterDescriptions(auth))
                                            .withSelfRel();
    
            Resources<Resource<CharacterDescription>> resources = new Resources<>(characters, allCharacterLink);
            return ResponseEntity.ok(resources);
        }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   gregturn    7 年前

    根据 the HAL spec 您可以呈现具有其内容和一组链接的单个资源,也可以呈现聚合资源,它在该资源中有空间容纳多个资源。

    在域模型中,可以清楚地显示多个文档,每个文档都有一个不同的 self URI( /characters/1 , /characters/2

    如果你阅读HAL规范,你会在下面找到这个定义 _embedded :

    .

    事实上,寻找这个词 阵列 在HAL规范中,只会引导您到上面引用的部分,并且 _links

    因此, _嵌入 是在HAL中呈现资源数组的适当位置。