3

I am struggling to have my multipart/form-data encoded in UTF-8 in RestTemplate entity. I don't know what am I doing wrong. Below I posted my code

LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("text", text);
map.add("id", id);

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Type","multipart/form-data;charset=UTF-8");

HttpEntity<LinkedMultiValueMap<String, String>> entity = new HttpEntity<>(map, httpHeaders);

ListenableFuture<ResponseEntity<String>> response = asyncRestOperations
                .postForEntity(url, entity, String.class);

When I insert any Polish letters into text parameter, for example: "ł" the rest template sends it as a "?". You can see it below, that in fact the body looks like this and "ł" gets converted into "?":

Content-Disposition: form-data; name="text"
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 1
?

I don't know why it is not working properly. How to change the default ISO-8859-1 encoding to UTF-8? Any help very appreciated!

Cheers.

SOLUTION FOUND:

I have modified slightly my AsyncRestTemplate and it no longer loses UTF-8 encoding. My working piece of code:

AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
List<HttpMessageConverter<?>> messageConverters = asyncRestTemplate.getMessageConverters();

StringHttpMessageConverter stringMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
AllEncompassingFormHttpMessageConverter allEncompassingConverter = new AllEncompassingFormHttpMessageConverter();
allEncompassingConverter.setCharset(Charset.forName("UTF-8"));
allEncompassingConverter.setMultipartCharset(Charset.forName("UTF-8"));
allEncompassingConverter.setPartConverters(Collections.singletonList(stringMessageConverter));

for (Iterator<HttpMessageConverter<?>> iterator = messageConverters.iterator(); iterator.hasNext(); ) {
    HttpMessageConverter conv = iterator.next();
    if (conv instanceof AllEncompassingFormHttpMessageConverter) {
        iterator.remove();
    }
}
messageConverters.add(allEncompassingConverter);
asyncRestTemplate.setMessageConverters(messageConverters);
dune76
  • 373
  • 1
  • 5
  • 10
  • I've tried adding the new StringHttpMessageConverter(Charset.forName("UTF-8")) to RestTemplate but this has no effect. – dune76 Sep 22 '16 at 17:11
  • Instead of remove/add messageConverters iterating on them, I extended default `RestTemplate` to initialize this `MyRestTemplate` with charsets (on FormHttp and StringHttp messageConverters) and used it on doing `doPost`, just for sake of clarity and keeping REST detailed stuff somewhere else. Just in case someone finds that useful... for @dune76, just adding the new converter seems to have no effect as the processing seems to use the first one is finds. Also, order of converters list matters: if you add at the end and remove the other of the same class, it will try using other converter O_O – Alfabravo Feb 01 '17 at 20:30
  • You'd put the solution as answer and accept it :) – Alfabravo Feb 01 '17 at 20:34

1 Answers1

0

Refer to the answer provided here Spring RestTemplate Charset UTF-8 does not work

For special character support, you need to encode your data.

Community
  • 1
  • 1
Rahul Kumar
  • 2,781
  • 1
  • 21
  • 29
  • 1
    In my case (communication with Twitter) encoding my data would not be a good idea. I already have the UTF-8 string so encoding it one more time doesn't make any sense. In this specific case the problem was that AsyncRestTemplate loses the UTF-8 encoding because all HttpMessageConverters defined inside are by default using ISO-8859-1. – dune76 Sep 23 '16 at 18:09
  • The solution provided in question by OP works without encoding data explicitly. Just setting up the messageConverters works well. – Alfabravo Feb 01 '17 at 20:34