使用 RestTemplate 的 Spring 数据休息响应中缺少链接
Missing links within Spring Data Rest Response using RestTemplate
当我调用 Spring 数据休息端点时,我希望看到每个对象中的自我链接和相关链接。 None 个链接出现。
RestTemplate 设置:
@HystrixCommand(fallbackMethod = "getFallbackScenicList")
@RequestMapping(value = "/s", method = RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
public PagedResources<Scenic> scenic() {
String url = "http://vr-dms-an-scenic/scenic";
ParameterizedTypeReference<PagedResources<Scenic>> ptr = new ParameterizedTypeReference<PagedResources<Scenic>>() {};
ResponseEntity<PagedResources<Scenic>> responseEntity =
this.restTemplate.exchange(url,HttpMethod.GET, null, ptr, 0,100
);
PagedResources<Scenic> resources = responseEntity.getBody();
return resources;
}
预期响应:
{
"_embedded": {
"scenic": [
{
"id": 1,
"name": "Test1 scenic",
"description": "This is a description1 for displaying information while in development",
"shortDescription": "Short Description Scenic1",
"_links": {
"self": {
"href": "http://localhost:49218/scenic/1"
},
"scenic": {
"href": "http://localhost:49218/scenic/1"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:49218/scenic"
},
"profile": {
"href": "http://localhost:49218/profile/scenic"
},
"search": {
"href": "http://localhost:49218/scenic/search"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
实际响应:
{
"_embedded": {
"scenic": [
{
"id": 1,
"name": "Test1 scenic",
"description": "This is a description1 for displaying information while in development",
"shortDescription": "Short Description Scenic1"
}
]
},
"_links": {
"self": {
"href": "http://localhost:49218/scenic"
},
"profile": {
"href": "http://localhost:49218/profile/scenic"
},
"search": {
"href": "http://localhost:49218/scenic/search"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
我假设 Scenic
不包含链接。所以而不是
PagedResources<Scenic>
你真的想要
PagedResources<Resource<Scenic>>
当我调用 Spring 数据休息端点时,我希望看到每个对象中的自我链接和相关链接。 None 个链接出现。
RestTemplate 设置:
@HystrixCommand(fallbackMethod = "getFallbackScenicList")
@RequestMapping(value = "/s", method = RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
public PagedResources<Scenic> scenic() {
String url = "http://vr-dms-an-scenic/scenic";
ParameterizedTypeReference<PagedResources<Scenic>> ptr = new ParameterizedTypeReference<PagedResources<Scenic>>() {};
ResponseEntity<PagedResources<Scenic>> responseEntity =
this.restTemplate.exchange(url,HttpMethod.GET, null, ptr, 0,100
);
PagedResources<Scenic> resources = responseEntity.getBody();
return resources;
}
预期响应:
{
"_embedded": {
"scenic": [
{
"id": 1,
"name": "Test1 scenic",
"description": "This is a description1 for displaying information while in development",
"shortDescription": "Short Description Scenic1",
"_links": {
"self": {
"href": "http://localhost:49218/scenic/1"
},
"scenic": {
"href": "http://localhost:49218/scenic/1"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:49218/scenic"
},
"profile": {
"href": "http://localhost:49218/profile/scenic"
},
"search": {
"href": "http://localhost:49218/scenic/search"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
实际响应:
{
"_embedded": {
"scenic": [
{
"id": 1,
"name": "Test1 scenic",
"description": "This is a description1 for displaying information while in development",
"shortDescription": "Short Description Scenic1"
}
]
},
"_links": {
"self": {
"href": "http://localhost:49218/scenic"
},
"profile": {
"href": "http://localhost:49218/profile/scenic"
},
"search": {
"href": "http://localhost:49218/scenic/search"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
我假设 Scenic
不包含链接。所以而不是
PagedResources<Scenic>
你真的想要
PagedResources<Resource<Scenic>>