spring版本:4.2.5.RELEASE
查看“org.springframework.http.converter.StringHttpMessageConverter”源碼,中有一段說明:
By default, this converter supports all media types ({@code */*}),
and writes with a {@code Content-Type} of {@code text/plain}. This can be overridden
by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
media type :(also MIME type and content type) is a two-part identifier for file formats and format contents transmitted on the Internet.
所以可以制定文件格式和編碼,
“text/html;charset=UTF-8”:解決瀏覽器直接訪問接口時中文亂碼問題。
“application/json;charset=UTF-8”:解決ajax【dataType:"json"】時中文亂碼問題。
在springmvc的配置文件中加入如下配置:
text/html;charset=UTF-8
application/json;charset=UTF-8
==================== 分割線 ?====================
其他還有各種中文亂碼問題:
1、jsp:聲明頁面編碼方式
登錄2、過濾器:在web.xml中加入如下代碼
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF8
encoding
/
3、修改tomcat的server.xml
總結:搞清楚出現亂碼的原因以及每一個配置的作用是解決出現亂碼的關鍵。
QQ技術交流群:282575808
--------------------------------------
聲明: 原創文章,未經允許,禁止轉載!
--------------------------------------