1 第一種情況(get接收參數):
最近在用solr做一個搜尋服務,釋出給手機和pc等用戶端調用,調用方式為: http://www.ganbo.search/search?q="手機"&rows=10&page=1.
表示本次搜尋關鍵字是“手機”,rows是每頁顯示10條,page是第一頁.這個時候在search項目的controller中接收搜尋條件q="手機"就會出現中文亂碼
解決方案之一: String queryString = new String(queryString.getbytes('iso8859-1'),'utf-8');(推薦)
解決方案之二: 修改tomcat 的配置檔案(不推薦)
解決方案隻三:....
2 第二種情況(使用responsbody傳回字元串中中文亂碼):
引起亂碼原因為spring mvc使用的預設處理字元串編碼為ISO-8859-1
解決方法一: 對于需要傳回字元串的方法添加注解(produces),如下:
@RequestMapping(value="/getUsers", produces = "application/json; charset=utf-8")
@ResponseBody
public String getAllUser(){
String result = "我愛中國";
return result;
}
此方法隻針對單個調用方法起作用。
解決方法二: 在配置檔案中加入
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>