Я пытаюсь переопределить GET-версию по умолчанию для API, чтобы добавить некоторые более конкретные детали.Spring Data Rest Overriding Default GET verb
Я создал restController и Get requestMapping:
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Resources<User>> getUsers(Pageable pageable) {
Page<User> users = userReposiotry.findAll(pageable);
Resources<User> userResources = new Resources<>(users);
return new ResponseEntity<Resources<User>>(userResources, HttpStatus.OK);
}
Он отлично работает, но он не возвращает ссылки и другую дополнительную информацию, которая была бы доступна, если не будут перезаписаны.
Обычай возвращается:
{
"_embedded": {
"users": [
{
"id": 1,
"username": "admin",
"registrationDate": "2015-11-18T21:04:54.000+0000",
"name": "Admin",
"email": "[email protected]",
"enabled": true,
"dateOfBirth": "2015-11-18",
"imageIdentifier": null,
"confirmationKey": ""
}]
}
}
В то время как оригинал:
{
"_embedded": {
"users": [
{
"id": 1,
"username": "admin",
"registrationDate": "2015-11-18T21:04:54.000+0000",
"name": "Admin",
"email": "[email protected]",
"enabled": true,
"dateOfBirth": "2015-11-18",
"imageIdentifier": null,
"confirmationKey": "",
"_links": {
"self": [
{
"href": "http://localhost:8080/api/users/1"
},
{
"href": "http://localhost:8080/api/users"
}
],
"user": {
"href": "http://localhost:8080/api/users/1{?projection}",
"templated": true
},
"inviteToApps": {
"href": "http://localhost:8080/api/users/1/inviteToApps"
},
"userRoles": {
"href": "http://localhost:8080/api/users/1/userRoles"
},
"currencyTokens": {
"href": "http://localhost:8080/api/users/1/currencyTokens"
},
"workoutAttendees": {
"href": "http://localhost:8080/api/users/1/workoutAttendees"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/users"
},
"profile": {
"href": "http://localhost:8080/api/profile/users"
},
"search": {
"href": "http://localhost:8080/api/users/search"
}
},
"page": {
"size": 20,
"totalElements": 7,
"totalPages": 1,
"number": 0
}
}
Как добавить отношение к обычаю, подобное в оригинале?
пружинные данные отдыха реализации HATEOAS по умолчанию. (Это механизм REST, который добавляет все ссылки там). Если вы переопределите его, вы должны сами реализовать HATEOAS на уровне контроллера. – horatius
Какую аннотацию вы указали на класс контроллера? @RestController? –