【问题标题】:HttpHeaders got changed in spring5 changes causing API to fail 400HttpHeaders 在 spring5 更改中发生更改,导致 API 失败 400
【发布时间】:2020-10-13 05:18:47
【问题描述】:

现在我正在将我们的 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);
}

现在在春季 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]}

到 [Accept:"application/json", Content-Type:"multipart/form-data;charset=UTF-8;boundary=lqBw1IeG3PhU9oYKiHGbhABo2SWZ6lBR", Content-Length:"37353"]

我搜索了很多发现一个类似的问题 这似乎更密切相关- Upgrading to Spring 5 broke RestTemplate MultipartFile upload

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

【问题讨论】:

  • 您应该将所有内容升级到相同版本,而不是应用程序的一部分!。还有你遇到的问题是什么?从问题中并不清楚。
  • 嗨,@M.Deinum 现在我们升级到 Spring 5.1.13.RELEASE 。在我们执行测试时发现文件上传不再有效。当看得更深。发现的标头现在与我们之前的完全不同。现在 API 响应 400。
  • 只有测试失败还是在您运行应用程序并进行实际上传时失败?还有你没有使用最新的 5.1.16(甚至 5.2.7)而是旧版本的原因吗?
  • 现在实际上传为 400。是的,我们不能更改春季版本。因为这是基于扩展的应用程序(更具体地说,如果我告诉它是 Hybris 1905),我们不能将其核心弹簧更改为 5.2.7。解决这个问题

标签: spring spring-mvc resttemplate multipart spring-resttemplate


【解决方案1】:

您好,找到了解决方案。

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

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。现在它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    相关资源
    最近更新 更多