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();
        }
    }


clipboard.png

阅读 393
评论 2019-06-22 提问
    1 个回答
    空无
    • 588


    response.setCharacterEncoding("UTF-8");
    换成
    response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
    就可以了

    评论 赞赏 2019-06-22
      撰写回答

      登录后参与交流、获取后续更新提醒