网站推广优化哪家公司好,今天重大新闻头条新闻国际新闻,国家电网网站制作,商城网站除了域名备案还要目录 问题描述问题解决参考文章 问题描述
正常接口的中文返回是正确的 而只要发生异常处理#xff0c;就会使用全局统一异常处理#xff0c;输出包含中文字符的json字符串#xff0c;发现#xff0c;全都变成了问号??????
/*** 统一异常处理*/
ControllerAdvice
Sl… 目录 问题描述问题解决参考文章 问题描述
正常接口的中文返回是正确的 而只要发生异常处理就会使用全局统一异常处理输出包含中文字符的json字符串发现全都变成了问号??????
/*** 统一异常处理*/
ControllerAdvice
Slf4j
public class GlobalExceptionHandler {ExceptionHandler(value Exception.class)public void exceptionHandler(Throwable error, HttpServletRequest request, HttpServletResponse response) throws IOException {PrintWriter writer response.getWriter();writer.write({\code\:1,\data\:null,\msg\:\请勿重复订阅\});}
}输出如下
{code:1,data:null,msg:??????}问题解决
设置响应头的内容字符编码即可
// 设置响应编码
response.setContentType(application/json;charsetutf-8);PrintWriter writer response.getWriter();
writer.write({\code\:1,\data\:null,\msg\:\请勿重复订阅\});输出如下
{code:1,data:null,msg:请勿重复订阅}参考文章
Servlet中使用JSON.toJSONString中文显示问号解决办法