0

现在我正在将我们的 spring 应用程序从 4 迁移到 5。我在 spring web 模块上遇到了特别的问题

spring-web-4.3.21.RELEASE.jar 到 spring-web-5.1.13.RELEASE.jar

我注意到 HttpHeaders 类的变化。在 spring-web-4.3.21.RELEASE.jar

HTTP标头在哪里

public HttpHeaders() {
    this(new LinkedCaseInsensitiveMap<List<String>>(8, Locale.ENGLISH), false);
}

现在在 Spring 5- 这更改为

public HttpHeaders() {
    this(CollectionUtils.toMultiValueMap(new LinkedCaseInsensitiveMap<>(8, Locale.ENGLISH)));
}

现在标题从 - {Accept=[application/json], Content-Type=[multipart/form-data;boundary=8HFYfdj_y58sNxrSdXenwlIQDsYiXS50], Content-Length=[51024]}

到 [接受:“应用程序/json”,内容类型:“multipart/form-data;charset=UTF-8;boundary=lqBw1IeG3PhU9oYKiHGbhABo2SWZ6lBR”,内容长度:“37353”]

我搜索了很多发现一个类似的问题,这似乎更密切相关- 升级到 Spring 5 破坏了 RestTemplate MultipartFile upload

有谁知道如何处理这个问题。

4

1 回答 1

0

嗨找到了解决方案。

当我们查看原始请求时发现了一些不同的东西


POST /XX/XXXX/XXXX HTTP/1.1
Accept: application/json
Content-Type: multipart/form-data;charset=UTF-8;boundary=3gOLqmQ6U7u3Iycd0lRnLnVa7dSIvAYqY7
Content-Length: 470
Host: localhost:XXXX
Connection: Keep-Alive
User-Agent: Apache-HttpClient/X.X.X (Java/11.0.X)
Accept-Encoding: gzip,deflate

--3gOLqmQ6U7u3Iycd0lRnLnVa7dSIvAYqY7
Content-Disposition: form-data; name="upload_file"
Content-Type: application/octet-stream
Content-Length: 48

[{"key":"291c0f23-712e-43p3-aq47-51899270a415"}]
--3gOLqmQ6U7u3Iycd0lRnLnVa7dSIvAYqY7
Content-Disposition: form-data; name="291c0f23-712e-43p3-aq47-51899270a415"; filename="291c0f23-712e-43p3-aq47-51899270a415"
Content-Type: application/octet-stream
Content-Length: 5


--3gOLqmQ6U7u3Iycd0lRnLnVa7dSIvAYqY7--


在以下 Content-Type: application/octet-stream 中添加了我们的请求。这使端点感到困惑。以前在旧版本的 spring resttemplate for upload_file 中没有指定 Content-Type。现在在更新版本的 spring 中添加了这个。现在我们将其更改为 application/json。现在它工作正常。

于 2020-06-29T12:46:22.067 回答