我将以下属性设置为 true "spring.hateoas.use-hal-as-default-json-media-type' 并添加 org.springframework.boot:spring-boot-starter-hateoas 作为依赖项。
代码
@Bean
public IntegrationFlow myUserFlow() {
return IntegrationFlows
.from(Http.inboundGateway("/user")
.requestMapping(r -> r.methods(HttpMethod.GET))
.get()
)
.handle((payload, headers) -> new MyUser("Joe Blogs")
.add(Link.of("http://localhost:8080/account", LinkRelation.of("account"))))
.get();
}
回复
GET http://localhost:8080/user
HTTP/1.1 200
connection: Keep-Alive, keep-alive
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 11 Jan 2021 18:31:13 GMT
Keep-Alive: timeout=60
{
"name": "Joe Blogs",
"links": [
{
"rel": "account",
"href": "http://localhost:8080/account"
}
]
}
Response code: 200; Time: 19ms; Content length: 87 bytes
我希望以 HAL 格式返回响应,例如
{
"name": "Joe Blogs",
"_links": [
{
"account":{
"href": "http://localhost:8080/account"
}
}
]
}
为什么不是这样?我怎样才能做到这一点?