Spring mvc 使用Gson 返回Json數據 中文亂碼問題
最近在學習java web 使用Intllij IDEA15.0.4創建一個maven管理的Spring mvc項目,我使用的是gson轉化爲Json返回,返回的中文顯示????,
{"industry":"?????","birthday":"2014-07-24","saying":"????"}
代碼如下:
/**
* Created by Marven on 2016/12/29.
*/
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
@ResponseBody()
public String index(){
return "welcome";
}
@RequestMapping(value = "/login",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
@ResponseBody()
public String login(HttpServletRequest request){
String name = request.getParameter("username");
String password = request.getParameter("password");
//調用業務處理LoginCheck
UserEntity entity=LoginCheck.login(name,password);
if(entity!=null){
;return JsonUtil.toJson(entity);
}else{
return "error";
}
}
}
網上有很多解決方法,都看着比較複雜,最後發現只要在註解的地方加一句話,就能輕鬆搞定
produces = "text/html;charset=UTF-8"