gpt4 book ai didi

spring - 如何使用 Spring HATEOAS 更改 HAL 链接格式

转载 作者:IT老高 更新时间:2023-10-28 13:48:52 26 4
gpt4 key购买 nike

我正在使用 Spring HATEOAS (0.16.0.RELEASE) 构建一个 Spring REST 应用程序,我希望 JSON 链接输出如下所示:

_links: {
self: {
href: "https://<ip>/api/policies/321"
}
}

虽然它呈现如下:

   "links":
[{
"rel":"self",
"href":"http://<ip>/api/policies/321"
}]

我正在使用 HATEOAS ResourceResourceAssembler

为什么我得到的是这种格式而不是另一种?怎么改?

最佳答案

In order to use HAL as the message format language for our RESTful API, and enable automatic pagination, we need some configuration changes in our applicaiton. Since Spring Data and Spring HATEOAS already provides annotations for configuration, all we need is to add those annotations:

@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
@EnableHypermediaSupport(type = { HypermediaType.HAL })
@ComponentScan(basePackages = {
"com.jiwhiz.rest"
})
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer c) {
c.defaultContentType(MediaTypes.HAL_JSON);
}
}

@EnableSpringDataWebSupport will add support for pagination and @EnableHypermediaSupport(type = { HypermediaType.HAL }) will add hypermedia support. Then we set default content type to application/hal+json.

引用:Design and Build RESTful API with Spring HATEOAS by 袁霁

关于spring - 如何使用 Spring HATEOAS 更改 HAL 链接格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25709672/

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