我有一个与此类似的问题,但是该解决方案未如我所愿:
Spring MVC how to create controller without return (String) view?
我有一个应该传递文件的表格:
及其控制器:
@PostMapping("/uploadFile")
public @ResponseBody void uploadFile(Model model, @RequestParam("file") MultipartFile multipartFile) throws InterruptedException {
//, RedirectAttributes redirectAttributes) throws InterruptedException {
Reservation reservation=new Reservation( );
fileService.uploadFile( multipartFile );
File file = new File("\\car-rental\\src\\main\\resources\\static\\attachments", multipartFile.getOriginalFilename());
log.info( "name and path " + file.getName() + file.getPath() );
Picname picname=new Picname();
picname.setPicnameAsString(file.getName() );
log.info( "picname file " + picname.getPicnameAsString() );
TimeUnit.SECONDS.sleep(2);
}
}
我希望控制器仅执行逻辑而不返回任何内容:但是它返回一个空页面:
我怎么让它不返回任何东西,只停留在表单上?唯一的想法是使用.sleep()设置延迟,但这将是一种解决方法,我想用更清洁的解决方案来解决它]
我有一个与此类似的问题,但是该解决方案无法按我希望的那样工作:Spring MVC如何在没有返回(字符串)视图的情况下创建控制器?我有一个应通过文件的表格:...
这是控制器的本质,因为您正在开发一个MVC应用程序,该响应将返回响应,该应用程序将向您指定的端点接收POST请求。
为了解决您的问题,您可能需要更改函数的返回类型。使用ResponseEntity返回类型可能比使用ResponseBody返回类型更合适。