代码之家  ›  专栏  ›  技术社区  ›  Stephane

如何从HAL响应中获取资源数据?

  •  2
  • Stephane  · 技术社区  · 6 年前

    我正在使用Spring Boot 2构建一个API,Angular 6客户端必须处理这样的响应,例如:

    {
      "_embedded" : {
        "userResourceList" : [ {
          "firstname" : "Stephane",
          "lastname" : "Toto",
          "email" : "toto@yahoo.se",
          "confirmedEmail" : false,
          "password" : "bWl0dGlwcm92ZW5jZUB5YWhvby5zZTptaWduZXRjNWRlMDJkZS1iMzIwLTQ4Y2YtOGYyMS0wMmFkZTQ=",
          "userRoles" : [ {
            "role" : "ROLE_ADMIN",
            "id" : 1
          } ],
          "_links" : {
            "self" : {
              "href" : "http://localhost:8080/api/users/1"
            },
            "roles" : {
              "href" : "http://localhost:8080/api/users/1/roles"
            }
          },
          "id" : 1
        } ]
      },
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/api/users"
        }
      },
      "page" : {
        "size" : 20,
        "totalElements" : 1,
        "totalPages" : 1,
        "number" : 0
      }
    }
    

    所有REST资源终结点都会重新运行这种模糊的响应。

    我想知道如何处理这些问题。如果有这样的库可以简化实际有效负载的解析和提取。

    我听说了 RestAngular 图书馆,但我不太确定这是我想要的。

    也许是一些侵入性较小的东西,只会帮助解析,比如:

    const user: User = hal2Json.parse<User>(response);
    

    更新:我没有找到任何这样的库,所以我求助于这个实现:

    public getSome(searchTerm: string, sortFieldName: string, sortDirection: string, currentPage: number, limit: number): Observable<any> {
      let httpParams = new HttpParams()
      .set('page', currentPage.toString())
      .set('size', limit.toString());
      if (searchTerm) {
        httpParams = httpParams.append('searchTerm', searchTerm);
      }
      if (sortFieldName && sortDirection) {
        httpParams = httpParams.append('sort', sortFieldName + ',' + sortDirection);
      }
      return this.httpService.get(this.usersUrl, httpParams);
    }
    
    export class UsersApi extends PaginationApi {
    
      constructor(users: User[], currentPageNumber: number, elementsPerPage: number, totalElements: number, totalPages: number) {
          super(currentPageNumber, elementsPerPage, totalElements, totalPages);
          this.users = users;
      }
    
      users: User[];
    
    }
    
    getUsers(searchTerm: string, sortFieldName: string, sortDirection: string, currentPageNumber: number): Observable<UsersApi> {
      return this.userService.getSome(searchTerm, sortFieldName, sortDirection, currentPageNumber, this.elementsPerPage)
        .pipe(
          map(response => {
            return new UsersApi(
              response._embedded.userResourceList as User[],
              this.paginationService.correctPageNumberMispatch(response.page.number),
              response.page.size,
              response.page.totalElements,
              response.page.totalPages
            );
          })
        );
    }
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   lainatnavi    4 年前

    定义一个接口来包装HAL响应。这就是我为图书服务所做的:

    // book.service.ts
    import { Injectable } from '@angular/core';
    import { Book } from '../book';
    import { Observable, map } from 'rxjs';
    import { HttpClient } from '@angular/common/http';
    
    @Injectable({
        providedIn: 'root'
    })
    export class BookService {
    
        private booksUrl = 'http://localhost:8080/books';
    
        constructor(private http: HttpClient) { }
    
        getBooks(): Observable<Book[]> {
            return this.http.get<GetBooksResponse>(this.booksUrl).pipe(
                map(response => response._embedded.bookList)
            );
        }
    }
    
    interface GetBooksResponse {
        _embedded: {
            bookList: Book[];
            _links: {self: {href: string}};
        };
    }
    

    HAL的回应是:
    enter image description here

    这篇文章值得赞扬:https://medium.com/@tbrouwer/spring-boot-backend-for-angular-tour-of-heroes-106dc33a739b

        2
  •  0
  •   danday74    6 年前

    你可以试试半红色的npm模块。似乎做了你想做的事:

    https://www.npmjs.com/package/halfred

    npm install halfred --save
    

    然后导入它:

    import * as 'halfred' from 'halfred' // something like this should work
    

    然后使用它:

    const resource = halfred.parse(object)