如何检索用于检索集合的实际排序顺序?

时间:2018-11-28 17:52:55

标签: spring-rest spring-hateoas angular7

我有一个检索用户集合的请求,如果请求特定的排序顺序,则可以对集合进行排序,例如:

http://localhost:8080/api/users?page=0&size=5&sort=email,asc

但是,如果没有在请求中显式传递排序顺序,则也会使用默认排序顺序对其进行排序,例如:

http://localhost:8080/api/users?page=0&size=5

在后一种情况下,按照控制器端点中指定的姓和名进行排序:

public ResponseEntity<PagedResources<UserResource>> all(@PageableDefault(sort = { "lastname", "firstname" }, direction = Sort.Direction.ASC) Pageable pageable, Sort sort,
        PagedResourcesAssembler<User> pagedResourcesAssembler, UriComponentsBuilder builder) {

因此,作为HATEOAS的HTTP响应如下:

{
  "_embedded" : {
    "userResourceList" : [
    ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/users?page=0&size=5&sort=firstname,asc"
    }
  },
  "page" : {
    "size" : 5,
    "totalElements" : 5,
    "totalPages" : 1,
    "number" : 0
  }
}

并且Location标头也是:

Location: http://localhost:8080/api/users?page=0&size=5&sort=lastname&sort=firstname&lastname.dir=ASC&firstname.dir=ASC

现在,在前端Angular客户端应用程序中,我想将此排序顺序反馈回某些页面属性,例如:

getUsers(searchTerm: string, sortFieldName: string, sortDirection: string, currentPageNumber: number): Observable<UsersApi> {
  return this.userService.getSome(searchTerm, sortFieldName, sortDirection, currentPageNumber, this.elementsPerPage)
    .pipe(
      map(response => {
        console.log(response.page.sortFieldName); // TODO How to have this one fed ?
        return new UsersApi(
          response._embedded.userResourceList as User[],
          response.page.number,
          response.page.size,
          response.page.totalElements,
          response.page.totalPages,
          response.page.sortFieldName,
          response.page.sortDirection
        );
      })
    );
}

但是,当然没有这样的response.page.sortFieldName属性。

我应该如何获得此实际排序顺序?我应该将其添加到响应中还是应该尝试解析Location标头?还是响应中的self网址值?

0 个答案:

没有答案