2016-01-23 3 views
2

Я пытаюсь переопределить 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 
    } 
} 

Как добавить отношение к обычаю, подобное в оригинале?

+0

пружинные данные отдыха реализации HATEOAS по умолчанию. (Это механизм REST, который добавляет все ссылки там). Если вы переопределите его, вы должны сами реализовать HATEOAS на уровне контроллера. – horatius

+0

Какую аннотацию вы указали на класс контроллера? @RestController? –

ответ

1

Просто вернуть обычай Bean, карты и т.д. вместо ResponseEntity:

@ResponseBody 
@RequestMapping(method = RequestMethod.GET) 
public Object getUsers(Pageable pageable) { 

    Page<User> users = userReposiotry.findAll(pageable); 
    Resources<User> userResources = new Resources<>(users); 

    List<MyCustomUser> customUsers = [...] 

    return customUSers; 
} 

// "some more specific details" in MyCustomUser 
public class MyCustomUser extends User{ 

    private List<Links> links; 

    // TODO: constructor, getter, setter. 
} 
0

Это потому, что вы возвращаете объекты, а не ресурсы. Попробуйте это:

public ResponseEntity<Resources<Resource<User>>> getUsers(Pageable pageable) { 

    Page<User> users = userReposiotry.findAll(pageable); 
    Resources<Resource<User>> userResources = Resources.wrap(users);