我真的不知道如何正确地将下面的调用转换为spring webflux webclient。
userIds是一个列表,我可以使用以下语法调用该服务,但我无法使用Spring WebFlux WebClient实现这一功能。如果你们中有谁知道怎么做,请帮帮我。
String url = "http://profile.service.com/v1/profiles/bulk";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
ResponseEntity<List<MiniProfile>> responseEntity;
try {
responseEntity = restTemplate.exchange(url, HttpMethod.POST, new
HttpEntity(userIds, headers), new
ParameterizedTypeReference<List<MiniProfile>>() {});
} catch (RestClientException e) {
responseEntity = new ResponseEntity<List<MiniProfile>>(HttpStatus.OK);
}
return responseEntity.getBody();这是我把它翻译成Webflux WebClient的方法:
Flux<String> flux = Flux.fromIterable(userIds);
return readWebClient.post().uri("/v1/profiles/bulk")
.body(BodyInserters.fromPublisher(flux, String.class))
.retrieve().bodyToFlux(MiniProfile.class);发布于 2018-05-18 12:56:04
您不应该将列表更改为flux,您应该将其作为列表发送,如下所示
return readWebClient.post()
.uri("/v1/profiles/bulk")
.syncBody(userIds)
.retrieve()
.bodyToFlux(new ParameterizedTypeReference<List<MiniProfile>>() {})
.flatMapIterable(Function.identity());此代码未经过测试,但原理是相同的
发布于 2019-12-06 15:40:54
使用.bodyValue(userIds)或.syncBody(userIds) (已弃用)代替带有正文插入器的正文
发布于 2021-01-21 14:41:08
**您可以参考下面提到的代码片段**
WebClient.post().uri(endPointUrl)
.contentType(MediaType.APPLICATION_XML)
.body(Mono.just(xmlEntity), String.class)
.retrieve()https://stackoverflow.com/questions/47483138
复制相似问题