2014-01-15 74 views
4

我正在使用配置了@EnableHypermediaSupport(type = HAL)的Spring Boot和Spring Hateoas。虽然这在基本场景中工作正常,但我希望能够向链接添加其他属性。例如,它可以容易地返回那将呈现链接像这样的链接:如何使用Spring Hateoas和HAL将其他属性添加到链接?

{ 
    "_links":{ 
     "self":{ 
     "href":"http://localhost/" 
     }, 
     "something":[ 
     { 
      "href":"http://some-url.com/something1" 
     }, 
     { 
      "href":"http://some-url.com/something2" 
     } 
     ] 
    } 

我想要做的是多个属性的东西相对增加的对象。例如:

{ 
    "_links":{ 
     "self":{ 
     "href":"http://localhost/" 
     }, 
     "something":[ 
     { 
      "name":"something1", 
      "href":"http://some-url.com/something1" 
     }, 
     { 
      "name":"something2", 
      "href":"http://some-url.com/something2" 
     } 
     ] 
    } 
} 

什么是没有创造我自己的DTO的做到这一点(最好使用ControllerLinkBuilder)的最好方法?我已经尝试创建我自己的链接子类,并为名称(以及getter和setter)添加字段,但它们似乎被忽略。

回答

4

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

我不知道你如何使用你的子类,但基本上这种方法的作品。您不能忘记您的name字段中的注释。例如:

public SuperLink extends Link { 
    @XmlAttribute 
    private String name; 

    public SuperLink(Link link, String name) { 
    super(link.getHref(), link.getRel()); 
    this.name = name; 
    } 
+0

我忽略了@XmlAttribute注释,但即使将其添加到字段(就像您暗示的),字段将不会呈现。你使用的是什么版本的Spring Hateoas? – Johan

+0

@Johan我使用0.8 – zeroflagL

+1

一个问题是我不小心在classpath中同时使用了更快的jackson和jackson 1。当我删除杰克森一个时,我得到以下错误: 引起:java.lang.IllegalStateException:冲突的属性名称定义:'链接'(对于[字段org.springframework.hateoas.ResourceSupport#链接])vs'_links'(对于[方法org.springframework.hateoas.ResourceSupport#getLinks(0 params)]) 但是,如果我升级到0.9.0.BUILD-SNAPSHOT一切似乎工作。 – Johan

相关问题