我的Range4应用程序使用Spring服务器,我使用的是HttpClient,我看到它返回对象,而不是像http一样的any。我读到了这个问题:Why does the httpclient in angular 4.3 return Object instead of any?和我明白了。我有许多类似于这样的领域的复杂的json:
{
"_embedded": {
"customers": [
{
"sid": "c44fdb6f-9b7c-4f75-8d4c-7542f54037b7",
"createdDate": "2017-08-01T13:06:23Z",
"lastModifiedDate": "2017-08-01T13:06:23Z",
"lastModifiedBy": "admin",
"name": "Customer 1",
"username": "customer1",
"address": "Via xxxxx",
"city": "Adria",
"landlinePhone": "+39042000000",
"mobilePhone": null,
"email": "email@test.it",
"fax": null,
"vatNumber": "IT01000020295",
"taxCode": null,
"iban": null,
"swift": null,
"enableEcommerce": true,
"balance": 0,
"enabled": true,
"new": false,
"_links": {
"self": {
"href": "....:8080/api/v1/customers/1"
},
"customer": {
"href": "....:8080/api/v1/customers/1"
},
"movements": {
"href": "....:8080/api/v1/customers/1/movements"
},
"country": {
"href": "....:8080/api/v1/customers/1/country"
}
}
}
]
},
"_links": {
"self": {
"href": "....:8080/api/v1/customers{?page,size,sort}",
"templated": true
},
"profile": {
"href": "....:8080/api/v1/profile/customers"
},
"search": {
"href": "....:8080/api/v1/customers/search"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}HttpClient不允许执行response.page.totalElements,因为正如我所说的,响应类型是一个对象,而不是any类型。我想知道实现我的目标的最佳方法是什么,我有两个想法:
any的响应。当然,在这种情况下,我可以不需要任何打字机就可以访问字段。你能给我一些建议和最佳实践,以达到我的目标和遵循的想法角的团队画?
发布于 2017-11-02 15:22:10
我也有同样的问题,我找到了一个可能的解决方案:
// Java: Spring Data REST Repository
@RepositoryRestResource(collectionResourceRel = "result", path = "customers")
public interface CustomerRepository extends PagingAndSortingRepository<Customer, Long> {
}
// TypeScript model
export interface ListResult<T> {
_embedded: EmbeddedList<T>;
_links: any;
page: any;
}
export interface EmbeddedList<T> {
results: T[];
}
export class Customer{
name: String;
... bla bla ...
}
// AJAX Call
http.get('/api/customers').subscribe(response => {
const customers: Customer[] = response._embedded.results;
});所有存储库都必须有collectionResourceRel="results".
https://stackoverflow.com/questions/45442927
复制相似问题