![](/img/trans.png)
[英]How to return an HTML page from a RESTful controller in Spring Boot?
[英]Is there a way to return HTML page from Restful Controller in Spring Boot?
我目前正在我的 spring 引导项目中上传文件。 I am using rest controller for my controller as that is what is written within the tutorial that I am using https://www.callicoder.com/spring-boot-file-upload-download-jpa-hibernate-mysql-database-example /
However, I found out that apparently rest controller unable to display HTML page based on what is written here: How to return a html page from a restful controller in spring boot?
有没有办法在保留 rest controller 的同时显示我的 HTML 页面?
这是我的controller
@RestController
public class RekonsiliasiController {
private static final Logger logger = LoggerFactory.getLogger(RekonsiliasiController.class);
@Autowired
private DBFileStorageService dbFileStorageService;
@RequestMapping("/rekonsiliasi")
public String index() {
System.err.println("MASUK PAK EKO OI");
return "rekonsiliasi";
}
@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
DBFile dbFile = dbFileStorageService.storeFile(file);
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/downloadFile/")
.path(dbFile.getId())
.toUriString();
return new UploadFileResponse(dbFile.getFileName(), fileDownloadUri,
file.getContentType(), file.getSize());
}
@GetMapping("/downloadFile/{fileId}")
public ResponseEntity<ByteArrayResource> downloadFile(@PathVariable String fileId) {
// Load file from database
DBFile dbFile = dbFileStorageService.getFile(fileId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(dbFile.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + dbFile.getFileName() + "\"")
.body(new ByteArrayResource(dbFile.getData()));
}
}
这是我的rekonsiliasi.html
<form method="POST" action="/uploadFile" enctype="multipart/form-data">
<input type="file" name="file" /><br/><br/>
<input type="submit" value="Submit" />
</form>
这就是我目前得到的,一个带有简单文本的空白页面
更新
我试图在索引和文件上传到以下内容之间进行划分。
RekonsiliasiController.java
@Controller
public class RekonsiliasiController {
@RequestMapping("/rekonsiliasi")
public String index() {
System.err.println("MASUK PAK EKO OI");
return "rekonsiliasi";
}
文件控制器.java
@RestController
public class FileController {
private static final Logger logger = LoggerFactory.getLogger(FileController.class);
@Autowired
private DBFileStorageService dbFileStorageService;
@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
System.err.println("CEK");
DBFile dbFile = dbFileStorageService.storeFile(file);
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/downloadFile/")
.path(dbFile.getId())
.toUriString();
return new UploadFileResponse(dbFile.getFileName(), fileDownloadUri,
file.getContentType(), file.getSize());
}
@GetMapping("/downloadFile/{fileId}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileId) {
// Load file from database
DBFile dbFile = dbFileStorageService.getFile(fileId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(dbFile.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + dbFile.getFileName() + "\"")
.body(new ByteArrayResource(dbFile.getData()));
}
}
现在 HTML 显示非常好,但是当我上传文件时出现 403 错误。 在我试图找到这部分的问题之前,我想知道是否有一些方法可以显示我的 HTML 页面,同时保留 rest controller?
编辑删除 FileController.Java 中的“uploadMultipleFiles”方法,它仍然给我 403 错误
首先,在您的 controller 方法uploadMultipleFiles
中的代码中,您实际上是直接调用 controller 方法uploadFile
,方法是将其视为另一种不理想的方法。 您需要递归调用另一个 controller 端点这一事实意味着它本身就是一个设计缺陷。 因此,尝试通过在服务层中提供处理这种情况的逻辑来删除这段代码。
其次,将视图 controller 和 rest controller 分成两个单独的类是正确的方法。 @RestController
注解设计为默认提供 json 响应,这就是为什么我们有@Controller
注解同时提供model 和 view的原因。 您收到的响应码 403 与此无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.