0

right now im migratingour spring application from 4 to 5. I'm facing issue with particular on spring web module

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

I noticed the changes in HttpHeaders class. In spring-web-4.3.21.RELEASE.jar

where HTTP Header was

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

Now on Spring 5- this is changed to

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

Now header is changed from- {Accept=[application/json], Content-Type=[multipart/form-data;boundary=8HFYfdj_y58sNxrSdXenwlIQDsYiXS50], Content-Length=[51024]}

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

I searched a lot found one similar issue this seems some more closely relevant - Upgrading to Spring 5 broke RestTemplate MultipartFile upload

Do anyone have any Idea how to deal this.

joe
  • 11
  • 2
  • You should upgrade everything to the same version not parts of the application!. Also what is the issue you are having? That isn't clear from the question. – M. Deinum Jun 23 '20 at 07:56
  • Hi, @M.Deinum right now we upgraded to Spring 5.1.13.RELEASE . while we performing the test found that file upload is no more working. when looked deeper. found header is totally different now what we had before.Now the API respond 400. – joe Jun 23 '20 at 08:04
  • Is it only the test that is failing or as well when you run the application and do the actual upload? ALso is there a reason you aren't using the latest 5.1.16 (or even 5.2.7) but an older release? – M. Deinum Jun 23 '20 at 08:06
  • Actual upload as repose is 400 now. ya we cant change spring version. as this is extension based application (more specifically if I tell it is Hybris 1905) and we cant change its core spring to 5.2.7. to just fix this – joe Jun 23 '20 at 08:08

1 Answers1

0

Hi Found the solution.

when we looked at the raw request found some different thing


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


In the following Content-Type: application/octet-stream got added in our request. this confused the endpoint. previously in older version of spring resttemplate for upload_file there was no Content-Type specified. now in newer version of spring this got added. Now we changed this to application/json. Now it is working fine.

joe
  • 11
  • 2