Spring boot SpringWebFlux网络客户端

Spring boot SpringWebFlux网络客户端,spring-boot,spring-webflux,Spring Boot,Spring Webflux,我真的不知道如何正确地将下面的调用转换为SpringWebFlux webclient userIds是列表,我可以使用以下语法调用该服务,但我无法使用SpringWebFlux WebClient。如果你们中有人知道怎么做,请帮助我 String url = "http://profile.service.com/v1/profiles/bulk"; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = ne

我真的不知道如何正确地将下面的调用转换为SpringWebFlux webclient

userIds是列表,我可以使用以下语法调用该服务,但我无法使用SpringWebFlux 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();
stringurl=”http://profile.service.com/v1/profiles/bulk";
RestTemplate RestTemplate=新RestTemplate();
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
反应性反应性;
试一试{
responseEntity=restemplate.exchange(url,HttpMethod.POST,新建
HttpEntity(用户标识、标题),新
ParameteredTypeReference(){});
}捕获(RestClientException e){
responseEntity=新的responseEntity(HttpStatus.OK);
}
返回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);
Flux-Flux=Flux.fromIterable(userid);
返回readWebClient.post().uri(“/v1/profiles/bulk”)
.body(BodyInserters.fromPublisher(flux,String.class))
.retrieve().bodyToFlux(MiniProfile.class);

您不应该将列表更改为flux,您应该将其作为如下列表发送

return readWebClient.post()
  .uri("/v1/profiles/bulk")
  .syncBody(userIds)
  .retrieve()
  .bodyToFlux(new ParameterizedTypeReference<List<MiniProfile>>() {})
  .flatMapIterable(Function.identity());
return readWebClient.post()
.uri(“/v1/profiles/bulk”)
.syncBody(用户标识)
.retrieve()
.bodyToFlux(新的参数化类型引用(){})
.flatMapIterable(Function.identity());

此代码未经测试,但原理相同

使用
.bodyValue(userIds)
.syncBody(userIds)
(已弃用),而不是使用body inserter的body

**您可以参考下面提到的代码片段**

WebClient.post().uri(endPointUrl)
           .contentType(MediaType.APPLICATION_XML)
          .body(Mono.just(xmlEntity), String.class)
          .retrieve()

您当前的解决方案中有哪些不起作用?你得到了什么结果?你在期待什么?我得到了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=debug
logging.level.reactor.ipc.Netty.http.client.HttpClient=debug
应用程序.properties
文件中谢谢你花时间回答我的问题,但您的代码或我的代码没有问题,但真正的问题是在我们的.NET堆栈上,当我们使用Webflux Webclient在.NET堆栈上放置或发布时,我们需要在.NET堆栈中处理原始请求。