如何在Spring Hateos中更改embbed集合的属性名称

Gee*_*rts 3 spring spring-hateoas

我正在使用Spring hateoas来生成HAL接口.我的代码看起来像这样:

@RequestMapping(method = RequestMethod.GET)
public Resources<Resource<Type>> all() {
    List<Resource<Type>> sdf = typeRepository.all().stream().map(type -> {
        return new Resource<Type>(type, ControllerLinkBuilder.linkTo(this.getClass()).slash(type.getId()).withSelfRel());
    }).collect(Collectors.toList());

    Resources<Resource<Type>> resourcesType = new Resources<>(sdf);
    resourcesType.add(ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(this.getClass()).all()).withSelfRel());
    return resourcesType;
}
Run Code Online (Sandbox Code Playgroud)

生成的json看起来像这样:

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/type"
    }
  },
  "_embedded": {
    "typeEntityList": [
      {
        "id": "4f7fa2da-20e2-4b42-9b45-2d1749825785",
        "version": 0,
        "name": "name1",
        "_links": {
          "self": {
            "href": "http://localhost:8080/type/4f7fa2da-20e2-4b42-9b45-2d1749825785"
          }
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我想更改"typeEntityList"的名称,但我无法找到它的来源或来源.谁知道怎么样?

Mat*_*nkt 14

看看这里:http: //docs.spring.io/spring-hateoas/docs/0.19.0.RELEASE/reference/html/#spis.rel-provider

你看到的是默认值.如果将EVO变形器放在类路径上,您将得到类似"类型"的东西.您还可以将@Relation注释放在实体上,并更改集合和单个资源的rel名称.

  • 感谢@Relation(collectionRelation = "types") 做到了,谢谢 (2认同)