詳解Spring MVC3返回JSON數(shù)據(jù)中文亂碼問題解決
查了下網(wǎng)上的一些資料,感覺比較復(fù)雜,這里,我這幾使用兩種很簡單的辦法解決了中文亂碼問題。
Spring版本:3.2.2.RELEASE
Jackson JSON版本:2.1.3
解決思路:Controller的方法中直接通過response向網(wǎng)絡(luò)流寫入String類型的json數(shù)據(jù)。
使用 Jackson 的 ObjectMapper 將Java對象轉(zhuǎn)換為String類型的JSON數(shù)據(jù)。
為了避免中文亂碼,需要設(shè)置字符編碼格式,例如:UTF-8、GBK 等。
代碼如下:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.fasterxml.jackson.databind.ObjectMapper; //Jsckson JSON Processer
import java.util.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.nio.charset.Charset;
/**
* Created with IntelliJ IDEA 12.0
* Date: 2013-03-15
* Time: 16:17
*/
@Controller
public class HomeController {
@RequestMapping(value="/Home/writeJson", method=RequestMethod.GET)
public void writeJson(HttpServletResponse response)
{
ObjectMapper mapper = new ObjectMapper();
HashMap<String,String> map = new HashMap<String,String>();
map.put("1","張三");
map.put("2","李四");
map.put("3","王五");
map.put("4", "Jackson");
String json = "";
try
{
json = mapper.writeValueAsString(map);
System.out.println(json);
//方案二
ServletOutputStream os = response.getOutputStream(); //獲取輸出流
os.write(json.getBytes(Charset.forName("GBK"))); //將json數(shù)據(jù)寫入流中
os.flush();
//方案一
response.setCharacterEncoding("UTF-8"); //設(shè)置編碼格式
response.setContentType("text/html"); //設(shè)置數(shù)據(jù)格式
PrintWriter out = response.getWriter(); //獲取寫入對象
out.print(json); //將json數(shù)據(jù)寫入流中
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
//return "home";
}
}
還有一種方法:設(shè)置 @RequestMapping 的 produces 參數(shù),代碼如下所示:
思路:使用 @ResponseBody 注解直接返回json字符串,為了防止中文亂碼,將@RequestMapping 的 produces 參數(shù)設(shè)置成"text/html;charset=UTF-8" 即可。
@RequestMapping(value="/Home/writeJson", method=RequestMethod.GET, produces = "text/html;charset=UTF-8")
@ResponseBody
public Object writeJson(HttpServletResponse response)
{
ObjectMapper mapper = new ObjectMapper();
HashMap<String,String> map = new HashMap<String,String>();
map.put("1","張三");
map.put("2","李四");
map.put("3","王五");
map.put("4", "Jackson");
String json = "";
try
{
json = mapper.writeValueAsString(map);
System.out.println(json);
}
catch(Exception e)
{
e.printStackTrace();
}
return json;
}
運(yùn)行結(jié)果如下圖所示:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java多態(tài)對象的類型轉(zhuǎn)換與動態(tài)綁定
這篇文章主要介紹了詳解Java多態(tài)對象的類型轉(zhuǎn)換與動態(tài)綁定,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
Java中比較器Comparator和Comparable的區(qū)別
這篇文章主要介紹了Java中比較器Comparator和Comparable的區(qū)別,我們在使用?Collections.sort()對鏈表進(jìn)行排序時,常常需要根據(jù)不同情況自定義排序規(guī)則,今天我們來看看比較器之間的區(qū)別,需要的朋友可以參考下2023-08-08
Spring事務(wù)@Transactional注解四種不生效案例場景分析
這篇文章主要為大家介紹了Spring事務(wù)@Transactional注解四種不生效的案例場景示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
如何設(shè)置IDEA遠(yuǎn)程連接服務(wù)器開發(fā)環(huán)境并結(jié)合cpolar實現(xiàn)ssh遠(yuǎn)程開發(fā)(最新推薦)
本文主要介紹如何在IDEA中設(shè)置遠(yuǎn)程連接服務(wù)器開發(fā)環(huán)境,并結(jié)合Cpolar內(nèi)網(wǎng)穿透工具實現(xiàn)無公網(wǎng)遠(yuǎn)程連接,然后實現(xiàn)遠(yuǎn)程Linux環(huán)境進(jìn)行開發(fā),感興趣的朋友跟隨小編一起看看吧2024-03-03
使用springcloud+oauth2攜帶token去請求其他服務(wù)
這篇文章主要介紹了使用springcloud+oauth2攜帶token去請求其他服務(wù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

