我想将多部分文件以及用户名和电子邮件地址传递给另一个控制器。当我尝试时,出现错误“必需的请求不是多部分文件”。

其余模板代码:

    val headers = LinkedMultiValueMap<String, String>()
    headers["x-apikey"] = apiKey
    headers["content-type"] = "multipart/form-data"

    println("updateFileHeaderStatusByIdFlowUrl : " + uploadCsvFileFlowUrl)

    val body = LinkedMultiValueMap<String, Any>()
    body.add("multipartFile", multipartFile)
    body.add("backofficeUsername", backofficeUsername)
    body.add("backofficeUserEmailAddress", backofficeUserEmailAddress)

    val requestEntity = HttpEntity<LinkedMultiValueMap<String, Any>>(body, headers)


    var response = restTemplate.exchange("localhost:8080/uploadCSVFile",
            HttpMethod.POST, requestEntity, String::class.java)

    System.out.println("response status: " + response.getStatusCode())
    System.out.println("response body: " + response.getBody())


REST API:

     @PostMapping("/uploadCSVFile/")
     fun uploadCSVFile(@RequestParam multipartFile: MultipartFile,
                  @RequestParam backofficeUsername: String,
                  @RequestParam backofficeUserEmailAddress: String) {}

最佳答案

尝试在方法签名中添加multipart/form-data

@PostMapping("/uploadCSVFile/", consumes = { MediaType.MULTIPART_FORM_DATA })
         fun uploadCSVFile(@RequestParam multipartFile: MultipartFile,
                      @RequestParam backofficeUsername: String,
                      @RequestParam backofficeUserEmailAddress: String) {}

10-06 09:09