I'm building an API using Spring Boot 2 and the Angular 6 client has to deal with responses such as this one for example :
{
"_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
}
}
All REST resources endpoints would retrun such HALified responses.
I wonder how to deal with these. If there is such library that eases the parsing and extraction of the actual payload.
I heard of the RestAngular library but I'm not so sure it's what I want.
Maybe something less invasive only assisting in the parsing like :
const user: User = hal2Json.parse<User>(response);
UPDATE: I didn't find any such library and so I resorted to this implementation:
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
);
})
);
}
