Java 当方法返回类型为void时,SpringBoot控制器中的CompletableFuture不工作
我在尝试返回SpringBoot控制器中的照片时发现以下问题(两种方法的行为相同,但第二种方法返回CompletableFuture) 方法1:不向outputStream发送任何内容,响应为http 200Java 当方法返回类型为void时,SpringBoot控制器中的CompletableFuture不工作,java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,我在尝试返回SpringBoot控制器中的照片时发现以下问题(两种方法的行为相同,但第二种方法返回CompletableFuture) 方法1:不向outputStream发送任何内容,响应为http 200 @RequestMapping(value = FIND_PHOTO, method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) public void getPhoto(HttpServletRespon
@RequestMapping(value = FIND_PHOTO, method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public void getPhoto(HttpServletResponse response, @PathVariable String id) {
ExceptionalConsumer<String, IOException> photoConsumer = photo -> {
ByteArrayInputStream bais = new ByteArrayInputStream(Base64.getDecoder().decode(photo));
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
StreamUtils.copy(bais, response.getOutputStream());
};
photoService.findPhoto(id) //This returns a CompletableFuture<String>
.thenAccept(photoConsumer);
}
@RequestMapping(value=FIND\u PHOTO,method=RequestMethod.GET,products=MediaType.IMAGE\u JPEG\u value)
public void getPhoto(HttpServletResponse,@PathVariable字符串id){
例外消费者照片消费者=照片->{
ByteArrayInputStream bais=新的ByteArrayInputStream(Base64.getDecoder().decode(photo));
response.setContentType(MediaType.IMAGE\u JPEG\u值);
copy(bais,response.getOutputStream());
};
photoService.findPhoto(id)//返回一个完整的未来
.然后接受(光消费者);
}
方法2:通过outputStream返回数据
@RequestMapping(value = FIND_PHOTO, method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public CompletableFuture<Void> getPhoto(HttpServletResponse response, @PathVariable String id) {
ExceptionalConsumer<String, IOException> photoConsumer = photo -> {
ByteArrayInputStream bais = new ByteArrayInputStream(Base64.getDecoder().decode(photo));
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
StreamUtils.copy(bais, response.getOutputStream());
};
return photoService.findPhoto(id)
.thenAccept(photoConsumer);
}
@RequestMapping(value=FIND\u PHOTO,method=RequestMethod.GET,products=MediaType.IMAGE\u JPEG\u value)
公共CompletableFuture getPhoto(HttpServletResponse,@PathVariable字符串id){
例外消费者照片消费者=照片->{
ByteArrayInputStream bais=新的ByteArrayInputStream(Base64.getDecoder().decode(photo));
response.setContentType(MediaType.IMAGE\u JPEG\u值);
copy(bais,response.getOutputStream());
};
返回photoService.findPhoto(id)
.然后接受(光消费者);
}
我认为spring有一种等待完成的CompletableFutures队列,但是如果方法的返回是void
,那么spring就没有意识到异步行为,然后我需要自己调用get()
或join()
有人能确认这是真实的行为还是我这边有错误吗?如果你没有处理异常,你就不应该在可完成的将来调用get()
Spring没有这个队列。如果您只需调用join()就不返回任何内容,并且从该方法返回的对象将在completablefuture时为T,或者您可以返回completablefuture,这样您的观察结果就正确了。什么数据方法2返回void completablefuture?第一个方法retrun type为void?@Deadpool数据在StreamUtils.copy(bais,response.getOutputStream())中返回;但如果返回类型为void,则不会调用该函数。如果愿意,您始终需要在
端点方法
上返回某些内容,否则返回void方法
将永远不会返回某些内容。你期待什么?我认为您混淆了异步编程
,就像Javascript
或其他使用异步的编程语言一样。@JonathanJohx您是否看到了StreamUtils.copy(bais,response.getOutputStream())代码>零件?即使返回是无效的,它也会发送信息,因此没有Javascript错误。请先理解问题并阅读代码,然后再给出一个无用的答案。Thx@mrossini那么我的问题是:如果我打算在控制器中返回一个完整的未来,spring何时以及如何调用get()?public void getPhoto(@Suspended AsyncResponse response,HttpServletResponse…return response.resume(completablefuture.join())