aim*_*ass 7 spring spring-data spring-data-rest spring-hateoas spring-webflux
这个问题曾经的标题是“Spring 5 Reactive WebClient 消费者 HAL+JSON HATEOAS PagedResources”,但新的标题更合适。
以下代码与 RestTemplate 完美配合(参数化类型从 Spring DataREST 提供的 HATEOAS/HAL PagedResource 返回):
// use exchange with ParameterizedTypeReference
ResponseEntity<PagedResources<Foo>> responseEntity =
restTemplate.exchange("/foos", HttpMethod.GET, null,
new ParameterizedTypeReference<PagedResources<Foo>>() {},
randomServerPort, 0, 100);
// then the actual list of foos can be obtained like so
PagedResources<Foo> resources = responseEntity.getBody();
List<foo> foos = new ArrayList(resources.getContent());
Run Code Online (Sandbox Code Playgroud)
这不适用于 Spring 5 Reactive WebClient:
public Mono<PagedResources<Foo>> getFoos() {
return client.get()
.uri("/foos").accept(MediaTypes.HAL_JSON)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<PagedResources<Foo>>(){});
}
Run Code Online (Sandbox Code Playgroud)
调用上述服务方法的控制器代码为:
@GetMapping("/foos")
public Mono<PagedResources<Foo>> getFoos() {
return dataService.getFoos();
}
Run Code Online (Sandbox Code Playgroud)
curl 的结果是:
{"links":[],"content":[],"page":null}
Run Code Online (Sandbox Code Playgroud)
经过大量研究,上面的代码应该可以工作,因为参数化类型应该传递给 Jackson,就像上面显示的 RestTemplate 的情况一样。
This is looking now more like a bug. But before filing one I would like to see if someone has WebClient bodyToXXX working with parametrized types (i.e. ParameterizedTypeReference, Super Type Tokens, etc.)
Note there is a test for this in the WebClient code but maybe the embedded type ref (PagedResources) is not currently supported or has a bug:
Bug filed: https://jira.spring.io/browse/SPR-16715
目前 Spring Data REST 和 Spring HATEOAS 都不支持 Reactor 类型。
| 归档时间: |
|
| 查看次数: |
4641 次 |
| 最近记录: |