如何使用Spring Hateoas和HAL向链接添加其他属性?

Joh*_*han 5 json spring-mvc spring-hateoas hal-json

我正在使用配置了@EnableHypermediaSupport(type = HAL)的Spring Boot和Spring Hateoas.虽然这在基本场景中运行良好,但我希望能够为链接添加其他属性.例如,很容易返回链接,这些链接将呈现如下链接:

{
   "_links":{
      "self":{
         "href":"http://localhost/"
      },
      "something":[
         {
            "href":"http://some-url.com/something1"
         },
         {
            "href":"http://some-url.com/something2"
         }
      ]
   }
Run Code Online (Sandbox Code Playgroud)

我想要做的是为rel中的对象添加更多属性.例如:

{
   "_links":{
      "self":{
         "href":"http://localhost/"
      },
      "something":[
         {
            "name":"something1",
            "href":"http://some-url.com/something1"
         },
         {
            "name":"something2",
            "href":"http://some-url.com/something2"
         }
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)

在不创建自己的DTO的情况下,最好的方法是什么(最好使用ControllerLinkBuilder)?我已经尝试创建自己的Link子类并为name(以及getter和setter)添加字段,但它们似乎被忽略了.

a b*_*ver 5

HAL 支持将得到重大升级,所以我会等待。

我不知道你如何使用你的子类,但基本上这种方法有效。您一定不要忘记您的name字段上的注释。例子:

public SuperLink extends Link {
  @XmlAttribute
  private String name;

  public SuperLink(Link link, String name) {
    super(link.getHref(), link.getRel());
    this.name = name;
  }
Run Code Online (Sandbox Code Playgroud)