response contentType值的问题

response contentType值的问题response,contentType,UTF-8,ISO-8859-1

大家好,又见面了,我是你们的朋友全栈君。

关于 reponse 返回类型 contentType 是 application/json;charset=ISO-8859-1 现象的阐述

现象发生描述:

在 Interceptor 的 preHandle 方法对 response 设置 contentType和charset

response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");

之后,发现了浏览器F12页面中的请求结果的 responseHeader 中 contentType 属性始终是 application/json;charset=ISO-8859-1 ,并不符合预期结果,最终结果的应该是 application/json;charset=UTF-8 才是理想的啊。

排查:

经过对上述代码代码以及上下文代码进行debug,最终通过源码逻辑获得到了编码始终不对的原因。

原因:

产生问题时的代码:

PrintWriter writer = response.getWriter();
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");

处理之后的代码:

response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();

对比代码可以看到,没错!就是 response.getWriter(); 这句代码的执行顺序导致的,它是在设置类型和编码之前还是之后!

那为什么先调用 response.getWriter(); 就会导致设置的编码类型不成功呢?来看源码:

org.apache.catalina.connector.Response#getWriter

    @Override
    public PrintWriter getWriter()
        throws IOException { 
   

        if (usingOutputStream) { 
   
            throw new IllegalStateException
                (sm.getString("coyoteResponse.getWriter.ise"));
        }
				// 看这个if,这个是问题产生的核心内容
        if (ENFORCE_ENCODING_IN_GET_WRITER) { 
   
            /* * If the response's character encoding has not been specified as * described in <code>getCharacterEncoding</code> (i.e., the method * just returns the default value <code>ISO-8859-1</code>), * <code>getWriter</code> updates it to <code>ISO-8859-1</code> * (with the effect that a subsequent call to getContentType() will * include a charset=ISO-8859-1 component which will also be * reflected in the Content-Type response header, thereby satisfying * the Servlet spec requirement that containers must communicate the * character encoding used for the servlet response's writer to the * client). */
            setCharacterEncoding(getCharacterEncoding());
        }

        usingWriter = true;
        outputBuffer.checkConverter();
        if (writer == null) { 
   
            writer = new CoyoteWriter(outputBuffer);
        }
        return writer;
    }

首先看注释,注释中就描述了“如果未制定response的编码,就默认为ISO-8859-1”。

并且会执行 usingWriter = true; 语句,记住这句,一会就用到了。

其次看代码 setCharacterEncoding(getCharacterEncoding()); 内容:

org.apache.catalina.connector.Response#getCharacterEncoding:
在这里插入图片描述

org.apache.catalina.connector.Response#setCharacterEncoding:
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161577.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月19日 下午4:46
下一篇 2022年7月19日 下午4:46


相关推荐

  • android开发 不注意的异常

    android开发 不注意的异常

    2022年1月29日
    46
  • pubsub机制_实现一个单例模式

    pubsub机制_实现一个单例模式PubSub是一种设计模式,中文叫发布订阅模式,简单来说就是消息发布者不直接向订阅者发布消息,而是发布到中介,而中介根据不同主题对消息进行过滤,并通知对该主题感兴趣的订阅者。该模式在前端现在很火的组件化开发十分常用,因为该模式松耦合,易于扩展的优点正式组件化开发所需要的。一个PubSub模型主要方法有3个,订阅,退订,发布,下面尝试在前端实现一个最简单的PubSub模块。varPub

    2025年7月9日
    4
  • 第四届CCCC团体程序设计天梯赛 赛后感

    第四届CCCC团体程序设计天梯赛 赛后感

    2021年9月27日
    56
  • JAVA面试基础「建议收藏」

    JAVA面试基础「建议收藏」JAVA面试部分重点内容目录JAVA面试部分重点内容五、输入输出流IO流  1.File类的常用方法?  2.说说IO流?  3.字节流的常用方法?  4.说说字符流?  5.说说缓冲流?  6.说说序列化和反序列化?五、输入输出流IO流  1.File类的常用方法?  java.io.File,使用该类的构造函数就可以创建文件对象,将硬盘中的一个具体的文件以Java对象的形式来表示。方法描述publicFile(Stringpathname)根据路径创建对象(是绝

    2022年7月7日
    24
  • AspNETPager的用法

    AspNETPager的用法nbsp 先下载 4 3 版 然后安装问题 1 在 vs net2005 中 该控件并不能自动添加到工具面板中 需要手动添加项 选定 AspNetPager dll 即可 2 在 codeBehind 的 cs 文件中 要 usingWuqi Webdiyer 3 写好 ChangePage 事件后 要与 aspnetpager 控件相关联以下是一段示例代码 前台 default aspx nbsp

    2026年3月16日
    3
  • 用过上百款编程MCP,只有这15个真正好用,Claude Code与Codex配置MCP详细教程

    用过上百款编程MCP,只有这15个真正好用,Claude Code与Codex配置MCP详细教程

    2026年3月16日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号