2

I'm using the Java Spring Resttemplate for getting a json via a get request. The JSON I'm getting has instead of special character slike ü ö ä or ß some weird stuff. So I guess somethings wrong with the character encoding. I can't find any help on the internet. The code I'm using for now is:

        String json = restTemplate.getForObject(
        overPassStatementPostCode,
        String.class,
        params);

The overPassStamementPostCode is just a string with placeholders. It gets filled with the parameters in the params Map.

The problem is that the String json doesn't have the wanted encoding.

Thank you for the help. Best regards Daniel

2
  • Can you log the response headers? Look for character encoding Commented Jan 5, 2015 at 8:28
  • the logged header is: {Date=[Mon, 05 Jan 2015 09:23:13 GMT], Server=[Apache/2.2.22 (Ubuntu)], Vary=[Accept-Encoding], Keep-Alive=[timeout=5, max=100], Connection=[Keep-Alive], Transfer-Encoding=[chunked], Content-Type=[application/json]} Commented Jan 5, 2015 at 9:27

4 Answers 4

1

You can set the encoding in HttpHeaders. Below code could help you:

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "text/html;charset=utf-8");

HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);

RestTemplate template = new RestTemplate();
ResponseEntity<String> response = template.exchange(
                "http://localhost/hello", HttpMethod.GET, requestEntity,
                String.class);
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for the help, but characters like üäö ß are still not encoded right. The code I'm using now is: HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("Accept", "text/html;charset=utf-8"); HttpEntity<?> requestEntity = new HttpEntity(requestHeaders); RestTemplate template = new RestTemplate(); ResponseEntity<String> json = template.exchange( overPassStatementPostCode, HttpMethod.GET, requestEntity, String.class,params);
Could you please try again after changing the charset to ISO-8859-1?
changed the charset in the requestHeaders.set method and still no luck.
logged header is : {Date=[Mon, 05 Jan 2015 09:23:13 GMT], Server=[Apache/2.2.22 (Ubuntu)], Vary=[Accept-Encoding], Keep-Alive=[timeout=5, max=100], Connection=[Keep-Alive], Transfer-Encoding=[chunked], Content-Type=[application/json]}
In the example code that I gave the content type is text/html in your case it should be application/json;charset=ISO-8859-1. Could you please confirm if you have made these changes?
|
0

Have you tried setting an encoding filter in web.xml?

<filter>
 <filter-name>encodingFilter</filter-name>
 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 <init-param>
     <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
 </init-param>
 <init-param>
     <param-name>forceEncoding</param-name>
     <param-value>true</param-value>
 </init-param>
</filter>
<filter-mapping>
 <filter-name>encodingFilter</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

1 Comment

Never used a web.xml for configuration. Have to read about this. I figure I have to registrate this as an applicationcontext?
0

Specify a converter that can convert from and to HTTP requests and responses. Here is an example.

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));

Comments

0

Upgrading to spring-web 5.2 solves the problem. or set writeAcceptCharset property to false belongs to StringHttpMessageConverter and use that convertor further in RestTemplate instance.

boolean writeAcceptCharSet = false;
List<HttpMessageConverter<?>> c = restTemplate.getMessageConverters();
for (HttpMessageConverter<?> mc : c) {
  if (mc instanceof StringHttpMessageConverter) {
    StringHttpMessageConverter mcc = (StringHttpMessageConverter) mc;
    mcc.setWriteAcceptCharset(writeAcceptCharSet);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.