英文:
Getting 415 error from POST to download file
问题
以下是您提供的内容的翻译:
我正在使用Spring Rest来下载一个zip文件,其内容由一系列文档ID确定。
我的控制器如下所示:
@RestController
@RequestMapping("api/zip-documents")
public class DocumentsRestController {
@Autowired
private DocumentDownloadService documentDownloadService;
@PostMapping(produces = {"application/zip"}, consumes = {"application/json"})
public ResponseEntity<Resource> downloadZip(@RequestBody List<String> documentIds) throws IOException {
// 将文档压缩到文件中
ByteArrayOutputStream outputStream = documentDownloadService.downloadZip(documentIds);
ByteArrayResource resource = new ByteArrayResource(outputStream.toByteArray());
return ResponseEntity.ok().header("Content-Disposition", "attachment; filename=\"file.zip\"")
.contentLength(outputStream.size()).body(resource);
}
}
我的测试使用Mockito如下,运行应用程序时也遇到相同的问题:
@Test
public void downloadZip_sunnyDayUseCase_contentTypeIsZip() throws Exception {
Mockito.when(documentDownloadService.downloadZip(Matchers.anyListOf(String.class)))
.thenReturn(new ByteArrayOutputStream());
mockMvc
.perform(post("/api/zip-documents")
.content("{ \"documentIds\": [\"123123\"] }"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/zip"));
}
我得到了一个HttpStatus 415的响应。这似乎是请求头的问题,因为我无法在restcontroler中设置断点。
英文:
I am using Spring Rest to download a zip file, then contents of which are determined by a list of document ids.
My Controller is like so
@RestController
@RequestMapping("api/zip-documents")
public class DocumentsRestController {
@Autowired
private DocumentDownloadService documentDownloadService;
@PostMapping(produces = {"application/zip"}, consumes = {"application/json"})
public ResponseEntity<Resource> downloadZip(@RequestBody List<String> documentIds) throws IOException {
// Zip the documents into a file
ByteArrayOutputStream outputStream = documentDownloadService.downloadZip(documentIds);
ByteArrayResource resource = new ByteArrayResource(outputStream.toByteArray());
return ResponseEntity.ok().header("Content-Disposition", "attachment; filename=\"file.zip\"")
.contentLength(outputStream.size()).body(resource);
}
}
My test, using Mockito is as follows, and I also get the same issue when running the app:
@Test
public void downloadZip_sunnyDayUseCase_contentTypeIsZip() throws Exception {
Mockito.when(documentDownloadService.downloadZip(Matchers.anyListOf(String.class)))
.thenReturn(new ByteArrayOutputStream());
mockMvc
.perform(post("/api/zip-documents")
.content("{ \"documentIds\": [\"123123\"] }"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/zip"));
}
I'm getting a HttpStatus 415 response. This seems to be an issue with the request header, as I cannot hit a breakpoint in the restcontroler.
答案1
得分: 3
HTTP状态码415
代表不支持的媒体类型
。由于您正试图向端点发布JSON数据,您可能希望在POST请求中的某个位置添加Content-Type: application/json
头部。
因此,在您的mockMvc
中,我猜您可以尝试以下操作:
mockMvc
.perform(post("/api/zip-documents")
.header("Content-Type", "application/json")
.content("{ \"documentIds\": [\"123123\"] }"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/zip"));
英文:
HTTP Status Code 415
stands for Unsupported Media Type
. Since you are trying to post JSON data to the endpoint, you probably want a Content-Type: application/json
header somewhere in your POST.
So, in your mockMvc
, I guess try the following:
mockMvc
.perform(post("/api/zip-documents")
.header("Content-Type", "application/json")
.content("{ \"documentIds\": [\"123123\"] }"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/zip"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论