SSM结合shiro在过滤器输出json中文乱码??
spring-MVC.XML配置
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>application/xml;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
shiro登入过滤器
@Override
protected boolean isAccessAllowed(ServletRequest request,
ServletResponse response, Object mappedValue) throws Exception {
User token = (User) SecurityUtils.getSubject().getPrincipal();
if(null != token || isLoginRequest(request, response)){// && isEnabled()
return Boolean.TRUE;
}
if (isAjax(request)) {// ajax请求
Map<String,String> resultMap = new HashMap<String, String>();
resultMap.put("status", "300");
resultMap.put("message", "会话超时,请重新登入");//当前用户没有登录!
System.out.println("ajax请求");
out(response, resultMap);
}
return Boolean.FALSE ;
}
public static void out(ServletResponse response, Map<String, String> resultMap){
PrintWriter out = null;
try {
response.setCharacterEncoding("UTF-8");
out = response.getWriter();
out.println(JSONObject.fromObject(resultMap).toString());
} catch (Exception e) {
System.out.println("json输出错误 ");
}finally{
if(null != out){
out.flush();
out.close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
把
response.setCharacterEncoding("UTF-8");
换成
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
就可以了