![](/img/trans.png)
[英]How to send application/json data along with file in postman multipart/form-data post request?
[英]Getting Spring to map when getting JSON along with a file using multipart/form-data or multipart/mixed
我有一个可行的解决方案,但需要它似乎很愚蠢。
这是我的工作解决方案:
@PreAuthorize("isAuthenticated()")
@ApiOperation(value = "Takes in a document.", nickname = "Document Upload", response = DocumentResponse[].class)
@ResponseStatus(HttpStatus.ACCEPTED)
@RequestMapping(
value = "/api/v1/document/upload",
produces = "application/json",
consumes = "multipart/form-data",
method = RequestMethod.POST)
public DocumentResponse uploadDocument(
// THIS is where I am using a String and don't want to.
@RequestPart("fileData") String fileData,
@RequestPart("file") MultipartFile file,
@RequestHeader("idempotency-id") String idempotencyId) throws IOException {
// THIS is the line I would also like to avoid.
DocumentUploadFileData fileDataObj = objectMapper.readValue(fileData, DocumentUploadFileData.class);
printBeanValues(fileDataObj);
More after....
问题是fileData
是一个String
对象。 我想让 spring 将 JSON 直接映射到我的DocumentUploadFileData
类,而不必自己做,如下所示: DocumentUploadFileData fileDataObj = objectMapper.readValue(fileData, DocumentUploadFileData.class);
我尝试过的:
@RequestPart
我什么也@RequestPart
。 其实我以为这样就好了。 不是。@RequestBody
。 我认为这肯定会奏效。 相反,它只是开始对我大喊大叫我的内容/类型无效? 内容类型没有改变,但出于某种原因,它希望它是 application/json(即使我明确地说是 multipart/form-data。我猜@ReqestBody
是针对 application/json 请求的,它不喜欢玩多部分?DocumentUploadFileData
它会失败并告诉我它没有对象的映射策略? 我认为这是一个多部分请求使 spring 决定使用可能需要添加的不同映射器这一事实? 我知道多部分请求使用边界并且通常有点不同,所以它可能需要不同的解决方案是有道理的。 我只是不知道如何提供。我已经 3 年没有使用 Spring 了,我确定解决方案并没有那么复杂,但是,几个小时后我仍然没有得到它。
尝试熟练使用此处提到的以下解决方案: Spring MVC Multipart Request with JSON
@RequestMapping(value = "/executesampleservice", method = RequestMethod.POST,
consumes = {"multipart/form-data"})
@ResponseBody
public boolean executeSampleService(
@RequestPart("properties") @Valid ConnectionProperties properties,
@RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file) {
return projectService.executeSampleService(properties, file);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.