我正在使用 Spring HATEOAS 构建 REST API。
经过一番尝试,我做到了这一点。
{
"_embedded": {
"organizations": [
{
"id": "3b2ef569-acd2-40dc-a57d-c1170f6c7429",
"name": "Google",
"organizationGroupId": "1d52c6d8-5f71-4ccc-b47a-a98da7eef700",
"_links": {
"self": {
"href": "http://localhost:8080/api/v1/organizations/3b2ef569-acd2-40dc-a57d-c1170f6c7429"
},
"organizations": {
"href": "http://localhost:8080/api/v1/organizations"
},
"organizationGroup": {
"href": "http://localhost:8080/api/v1/organization-groups/1d52c6d8-5f71-4ccc-b47a-a98da7eef700"
}
}
}
]
},
"_links": {
"first": {
"href": "http://localhost:8080/api/v1/organizations?page=1&size=10&sort=createdAt,asc"
},
"prev": {
"href": "http://localhost:8080/api/v1/organizations?page=1&size=10&sort=createdAt,asc"
},
"self": {
"href": "http://localhost:8080/api/v1/organizations?page=2&size=10&sort=createdAt,asc"
},
"last": {
"href": "http://localhost:8080/api/v1/organizations?page=2&size=10&sort=createdAt,asc"
}
},
"page": {
"size": 10,
"totalElements": 11,
"totalPages": 2,
"number": 2
}
}
我不喜欢“_embedded”和“_links”。有什么办法可以更改这些名称吗?或者这些是我们应该在 Spring 中使用的标准?
我可以通过在 Dto 类上使用
@Relation(collectionRelation = "organizations")
注释来更改集合名称。但找不到任何重命名“_embedded”的东西。
该项目为 Spring HATEOAS 提供 JSON:API 媒体类型支持。
https://toedter.github.io/spring-hateoas-jsonapi/release/reference/index.html
正确的方法是自定义媒体类型https://youtu.be/o4cc6KzNrpI?list=PL54DWbEqNyYmEXWtKIxWUtV-cIYqJuQCW&t=2700如果有人想要其他格式。