Spring RestTemplate中文乱码原因

2014-11-24 09:53:37 · 作者: · 浏览: 0


因为RestTemplate类中的构造函数RestTemplate() 里

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

这句中StringHttpMessageConverter的这个类,默认是ISO-8859-1:
public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");

解决方法:

把StringHttpMessageConverter这个类的源码中的
Charset.forName("ISO-8859-1");
改为
Charset.forName("utf-8");


编译后重新放在org.springframework.web-3.0.5.RELEASE.jar中即可

还有另外一种方式,使用配置方式,在你你的sping-servlet.xml中写入:

vc.annotation.AnnotationMethodHandlerAdapter" >






text/html;charset=UTF-8






这样子服务器端的spring编码方式就是utf-8了。