Spring 数据剩余覆盖默认 GET 动词

Spring Data Rest Overriding Default GET verb

我正在尝试覆盖 API 的默认 GET 动词以添加一些更具体的细节。

我已经创建了一个 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);
}

它很好用,但它没有 return 如果不被覆盖则可用的链接和其他附加信息。

自定义 returns:

{
  "_embedded": {
    "users": [
      {
        "id": 1,
        "username": "admin",
        "registrationDate": "2015-11-18T21:04:54.000+0000",
        "name": "Admin",
        "email": "admin@admin.com",
        "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": "admin@admin.com",
        "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
  }
}

怎样才能像原来那样把关系添加到自定义的?

那是因为您 return 实体,而不是资源。试试这个:

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

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

只是 return 自定义 Bean、Map 等而不是 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.
}