用springmvc作接口时返回json数据中文乱码
? ? ? ? ? ?以前都是用springmvc做web項目,瀏覽器訪問,所以沒出現過fastjson中文亂碼的問題,今天要做一個接口,結果杯具了中文亂碼。
? ? ? ? ? 接口調用拿到返回數據瘋狂的修改編碼,結果無效,考慮是不是json封裝時就出現了亂碼了。結果一看,果然是得。后來找到如下方法得以解決:
@RequestMapping(value="/getWork.html" ,method=RequestMethod.POST)
public ResponseEntity<String> ?getWork(@Valid ViewYyRecord record,HttpServletResponse response) throws UnsupportedEncodingException {
JSONObject ret=workService.getWork(record);
System.out.println(ret.toString());
HttpHeaders headers = new HttpHeaders(); ??
? ? ? ? MediaType mediaType=new MediaType("text","html",Charset.forName("GBK")); ??
? ? ? ? headers.setContentType(mediaType); ??
? ? ? ? ResponseEntity<String> responseEntity =new ResponseEntity<String>(ret.toString(),headers,HttpStatus.OK); ??
? ? ? ? return responseEntity; ?
}
返回的是ResponseEntity<String>其實還是json字符串,很方便。贊!
總結
以上是生活随笔為你收集整理的用springmvc作接口时返回json数据中文乱码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现图片验证码,其实就是简单的验证码实现
- 下一篇: springmvc返回数据中文乱码