Chinese garbled characters in Spring RestTemplate

Source: Internet
Author: User


Because in the RestTemplate class, the constructor RestTemplate ()

This. messageConverters. add (new StringHttpMessageConverter ());

The StringHttpMessageConverter class in this sentence, the default is the ISO-8859-1:
Public static final Charset DEFAULT_CHARSET = Charset. forName ("ISO-8859-1 ");

 

Solution:

In the source code of the StringHttpMessageConverter class
Charset. forName ("ISO-8859-1 ");
Change
Charset. forName ("UTF-8 ");


Compile and re-place it in org. springframework. web-3.0.5.RELEASE.jar.

 

There is another way to write in your sping-servlet.xml using the configuration method:

'<Bean class = "org. springframework. web. servlet. mvc. annotation. AnnotationMethodHandlerAdapter">

<Property name = "messageConverters">
<List>
<Bean class = "org. springframework. http. converter. StringHttpMessageConverter">
<Property name = "supportedMediaTypes">
<List>
<Value> text/html; charset = UTF-8 </value>
</List>
</Property>
</Bean>
</List>
</Property>
</Bean>


In this way, the server-side spring encoding method is UTF-8.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.