我的Angular 4应用程序使用Spring Data REST API HATEOAS服务器,我使用的是HttpClient,我发现它返回Object而不是像http这样的any
。 我看过这个问题:Why does the httpclient in angular 4.3 return Object instead of any? 我知道了。我有很多这样的领域复杂的JSON:Angular 4 HttpClient与HATEOAS REST服务器
{
"_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 protected]",
"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
。在这种情况下,我当然可以在没有任何类型检查的情况下访问字段。创建几个对象来映射HATEOAS响应:可能我需要至少2-3个接口,每个模型实体需要1个类。 与第一种解决方案相比,这种方法看起来相当复杂,可能过于复杂,我总是需要从对象到我的特定实体模型进行强制转换;这意味着由于错误类型转换,我可能会产生运行时异常。
你能不能给我一些建议和最佳实践,以达到自己的目标,然后按照这个想法角的团队画了?
您是否尝试了回复['page']。totalElements? – Mackelito