gpt4 book ai didi

json - 如何使用 Spring Hateoas 和 HAL 向链接添加附加属性?

转载 作者:行者123 更新时间:2023-12-04 20:08:57 24 4
gpt4 key购买 nike

我正在使用配置了 @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"
}
]
}

我想要做的是为 something rel 中的对象添加更多属性。例如:
{
"_links":{
"self":{
"href":"http://localhost/"
},
"something":[
{
"name":"something1",
"href":"http://some-url.com/something1"
},
{
"name":"something2",
"href":"http://some-url.com/something2"
}
]
}
}

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

最佳答案

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

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

public SuperLink extends Link {
@XmlAttribute
private String name;

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

关于json - 如何使用 Spring Hateoas 和 HAL 向链接添加附加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21143694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com