小编Bk *_*ago的帖子

如何正确读取Flux <DataBuffer>并将其转换为单个inputStream

我正在为我的spring-boot应用程序使用WebClient和自定义BodyExtractor类

WebClient webLCient = WebClient.create();
webClient.get()
   .uri(url, params)
   .accept(MediaType.APPLICATION.XML)
   .exchange()
   .flatMap(response -> {
     return response.body(new BodyExtractor());
   })
Run Code Online (Sandbox Code Playgroud)

BodyExtractor.java

@Override
public Mono<T> extract(ClientHttpResponse response, BodyExtractor.Context context) {
  Flux<DataBuffer> body = response.getBody();
  body.map(dataBuffer -> {
    try {
      JaxBContext jc = JaxBContext.newInstance(SomeClass.class);
      Unmarshaller unmarshaller = jc.createUnmarshaller();

      return (T) unmarshaller.unmarshal(dataBuffer.asInputStream())
    } catch(Exception e){
       return null;
    }
  }).next();
}
Run Code Online (Sandbox Code Playgroud)

上面的代码使用小的有效负载而不是大的有效负载,我认为这是因为我只读取一个通量值,next我不知道如何组合和读取所有dataBuffer.

我是反应堆的新手,所以我不知道很多使用flux/mono的技巧.

java spring project-reactor reactive-streams spring-webflux

12
推荐指数
4
解决办法
1万
查看次数

如何在春季以被动方式提供文件/ PDF文件

我有以下端点代码可提供PDF文件。

@RequestMapping
ResponseEntity<byte[]> getPDF() {
  File file = ...;
  byte[] contents = null;
  try {
    try (FileInputStream fis = new FileInputStream(file)) {
      contents = new byte[(int) file.length()];
      fis.read(contents);
    }
  } catch(Exception e) {
    // error handling
  }
  HttpHeaders headers = new HttpHeaders();
  headers.setContentDispositionFormData(file.getName(), file.getName());
  headeres.setCacheControl("must-revalidate, post-check=0, pre-check=0");
  return new ResponseEntity<>(contents, headers, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)

我如何才能将上面转换为反应类型Flux/Mono和DataBuffer。

我有支票,DataBufferUtils但似乎没有提供我需要的东西。我也没有找到任何例子。

java spring-boot reactive spring-webflux

4
推荐指数
1
解决办法
2797
查看次数