RestTemplate encoding issue

When getting data from one of the web service quotes (") appears as (?) When I use the rest pattern. I tested the web service in postman in chrome and gave the correct characters. I tried UTF-8 encoding but no success.

I checked the following, coding from web service provider:

Cache Control -> Private Connection -> Close Content-Encoding -> gzip Content-Length -> 3407 Content-Type -> text / xml; encoding = ISO-8859-1 Date β†’ Wed, 10 Jun 2015 13:35:53 GMT Server β†’ Google Search Appliance Vary β†’ Accept-Encoding X-Frame-Options β†’ SAMEORIGIN x-content-type-options β†’ nosniff x-xss- protection β†’ 1; Mode = block

Here is my code:

RestTemplate restTemplate = new RestTemplate ();

    HttpHeaders headers = new HttpHeaders();
    MediaType mediaType = new MediaType("text", "xml", Charset.forName("ISO-8859-1"));

    headers.set("Accept", "text/xml; charset=ISO-8859-1");
    headers.setContentType(mediaType);
    headers.setAcceptCharset(Arrays.asList(Charset.forName("UTF-8")));
    headers.setAccept(Arrays.asList(mediaType));

    ResponseEntity<String> res = restTemplate.exchange(gsaSearchUrl, HttpMethod.GET, new HttpEntity<String>(headers), String.class);


    System.out.println(res.getBody());

      

+3


source to share


1 answer


You need to add the HttpMessageConverter for UTF-8 encoded strings to your RestTemplate. Something like this will do it for you:



    restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

      

0


source







All Articles