Spring Web:如何传输多个多部分文件和 JSON 到@RestController

Spring Web: How to transfer many multipart file and JSON to @RestController

我很难创建 spring 控制器,它使用 MultipartFile 数组和 JSON。

我已经为 JSON 和 一个 多部分文件完成了控制器:

控制器:

@PostMapping("/upload")
@ApiOperation("Загрузить новый файл")
@ResponseBody
public Map uploadFile(@RequestPart("file") MultipartFile file,
                      @RequestPart(required = false) JsonObject json) throws IOException {
    UUID fileId = fileService.save(file);
    UUID jsond = jsonService.save(json);

    return ImmutableMap.of("fileId", fileId, "jsond", jsond );
}                                                                                               

配置文件:

@Component
public class JsonConventerConfig extends AbstractJackson2HttpMessageConverter {

    protected JsonConventerConfig(ObjectMapper objectMapper) {
        super(objectMapper, MediaType.APPLICATION_OCTET_STREAM);
    }

    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        return false;
    }

    @Override
    public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {
        return false;
    }

    @Override
    protected boolean canWrite(MediaType mediaType) {
        return false;
    }
}

但我不知道如何使用 MultipartFile 调整我的控制器。

谢谢!

在@RequestPart中添加一个数组即可解决问题

@PostMapping(value = "/file")
@ApiOperation("Загрузить новый файл")
@ResponseBody
public Set<UUID> uploadFile(@RequestPart MultipartFile[] files,
                            @RequestPart(required = false) Document document) throws IOException

事实证明 swagger 不支持多部分请求,所以我在测试期间遇到异常。