简体   繁体   English

使用 multipart/form-data 或 multipart/mixed 获取 JSON 和文件时让 Spring 进行映射

[英]Getting Spring to map when getting JSON along with a file using multipart/form-data or multipart/mixed

I have a working solution but it seems silly that it's needed.我有一个可行的解决方案,但需要它似乎很愚蠢。

This is my working solution:这是我的工作解决方案:

@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....

The trouble is that fileData is a String object.问题是fileData是一个String对象。 I would like to have spring map the JSON directly to my DocumentUploadFileData class without having to do it myself as seen here: DocumentUploadFileData fileDataObj = objectMapper.readValue(fileData, DocumentUploadFileData.class);我想让 spring 将 JSON 直接映射到我的DocumentUploadFileData类,而不必自己做,如下所示: DocumentUploadFileData fileDataObj = objectMapper.readValue(fileData, DocumentUploadFileData.class);

What I have tried:我尝试过的:

  1. Instead of @RequestPart I just used nothing.而不是@RequestPart我什么也@RequestPart I actually thought that would be fine.其实我以为这样就好了。 It wasn't.不是。
  2. List item @RequestBody .列表项@RequestBody I thought for sure this would work.我认为这肯定会奏效。 Instead, it just started yelling at me about my content/type not being valid?相反,它只是开始对我大喊大叫我的内容/类型无效? The content type didn't change but for some reason it wanted it to be application/json (even though I'm saying multipart/form-data explicitly. I guess @ReqestBody is intended for application/json requests and it doesn't like playing with multipart?内容类型没有改变,但出于某种原因,它希望它是 application/json(即使我明确地说是 multipart/form-data。我猜@ReqestBody是针对 application/json 请求的,它不喜欢玩多部分?
  3. I also tried using RequestParam, that actually works if I use a String as the object.我也尝试使用 RequestParam,如果我使用 String 作为对象,它实际上有效。 If I try to use my DocumentUploadFileData instead it fails and tells me it doesn't have a mapping strategy for the object?如果我尝试使用我的DocumentUploadFileData它会失败并告诉我它没有对象的映射策略? I think something about the fact that it's a multipart request makes spring decide to use different mappers that maybe I need to add?我认为这是一个多部分请求使 spring 决定使用可能需要添加的不同映射器这一事实? I know multipart requests use boundaries and are just a little different generally so it makes sense that it might want a different solution.我知道多部分请求使用边界并且通常有点不同,所以它可能需要不同的解决方案是有道理的。 I just don't know how to provide that.我只是不知道如何提供。

I haven't used Spring for 3 years I'm sure the solution isn't that complicated, however, I still haven't gotten it after several hours.我已经 3 年没有使用 Spring 了,我确定解决方案并没有那么复杂,但是,几个小时后我仍然没有得到它。

Try to adept the following solution noted here: Spring MVC Multipart Request with JSON尝试熟练使用此处提到的以下解决方案: 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.

相关问题 如何在 postman 多部分/表单数据发布请求中将应用程序/json 数据与文件一起发送? - How to send application/json data along with file in postman multipart/form-data post request? Spring RestTemplate:多部分/表单数据禁止获取 403,但使用 curl - Spring RestTemplate: getting 403 forbidden for multipart/form-data but working with curl 如何从 Spring WebFlux 中的 Multipart/form-data 中的 stream 文件 - How to stream file from Multipart/form-data in Spring WebFlux 如何使用 Spring MockMvc 放置多部分/表单数据? - How to PUT multipart/form-data using Spring MockMvc? 多部分/表单数据发布-Java Spring - Multipart/Form-Data Post - Java Spring 405 错误:带有 Spring 的多部分/表单数据 - 405 Error : multipart/form-data with Spring 解析 spring 中的多部分/表单数据响应 - parse multipart/form-data response in spring 如何在 Spring 引导中通过 REST API 使用请求正文(JSON)以及多个文件(多部分/表单数据)? - How to consume Request Body (JSON) along with multiple files (multipart/form-data) via REST API in Spring Boot? 使用Jackson解析multipart / form-data或application / json - Using Jackson to parse multipart/form-data or application/json 在Java中使用Restlet multipart / form-data上载文件 - Upload a file using Restlet multipart/form-data in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM