0

how can i decode UTF-8 in the ResponseEntity (especially when i get Umlauts). I also tried with SpringHttpMessageConverter, but this is not working.

RestTemplate restTemplate = new RestTemplate();
//this is not working only by POST
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

ResponseEntity<List<WebUser>> responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,
                        requestEntity, new ParameterizedTypeReference<List<WebUser>>() {
                        });

//responseEntity.getBody().get(0).getFirstName() should be Müller but I get M&uuml;ler
trap
  • 2,550
  • 7
  • 21
  • 42

1 Answers1

2

should be Müller but I get M&uuml;ler

It's not an UTF-8 issue. &uuml; is an HTML entity (see here for the list of entities). You should unescape HTML entities in response to get UTF-8 strings that you want (see this answer on how to do that using Apache Commons).

Community
  • 1
  • 1
borowis
  • 1,207
  • 10
  • 17
  • Is there no other way then StringEscapeUtils. As you can see I have List of object, which I pass to my fronted application. (the whole list). With StringEscapeUtils I have to check every string individual. – trap Oct 24 '16 at 11:47
  • There are a couple of considerations: 1. consider normalizing data to UTF8 strings when storing them to DB so that you have umlauts in the database already 2. consider doing html unescaping on frontend (if that's too hard / too late to do 1.) – borowis Oct 24 '16 at 12:00