【发布时间】:2018-05-09 01:13:32
【问题描述】:
我真的不知道如何正确地将以下调用转换为 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);
【问题讨论】:
-
您当前的解决方案有什么问题?你得到什么结果?你期待的是什么?
-
我收到一个 500 错误代码,我不知道它来自哪里,这里是堆栈跟踪。 org.springframework.web.reactive.function.client.WebClientResponseException:ClientResponse 有错误的状态代码:500 内部服务器错误。我希望我们的服务返回一个配置文件列表。请注意带有 RestTemplate 的旧代码对我来说效果很好。
-
这是属于您的问题的重要信息,真的。这表示服务器使用 HTTP 500 进行响应。因此 RestTemplate 和 WebClient 之间的请求必须不同。
-
我同意 RestTemplate 和 WebClient 之间的请求必须不同。我一直在尝试查看它们之间的请求是否不同,但我只能看到 RestTemplate 的请求正文被翻译为这样的字符串数组 ["0449b652-0006-0000-0000-000000000000", "0333b652- 0006-0000-0000-000000000000"] 与我们的依赖服务配合得很好,但我无法将 Netty WebClient 中的请求视为像 RestTemplate 这样的 json 格式,所以我不知道有什么区别。你知道如何在 Netty WebClient 中看到序列化的请求正文吗?
-
您可以像这样设置日志记录级别:
logging.level.reactor.ipc.netty.channel.ContextHandler=debuglogging.level.reactor.ipc.netty.http.client.HttpClient=debug在您的application.properties文件中
标签: spring-boot spring-webflux