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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Python模拟一个用户登录系统

    Python模拟一个用户登录系统题目:模拟一个用户登录系统,用户输入用户名和密码,输入正确即可进入系统直接上代码:#模拟一个用户登录系统,用户输入用户名和密码,输入正确即可进入系统d=[‘yao’,’123456′]while1:name=input("请输入用户名:")ifnameind:breakelse:print("你输入的用户名不存在,请…

    2022年5月17日
    43
  • pycharm 有些库(函数)没有代码提示

    pycharm 有些库(函数)没有代码提示问题描述如图,输入变量im.后没有关于第三方库相应的函数或其他提示解决方案因为python是弱类型语言,IDE无法判断Image.open(&amp;amp;amp;amp;amp;quot;Me.jpg&amp;amp;amp;amp;amp;quot;)的返回值类型,无法根据参数类型自动补全类型注解指定类型3.使用isinstance指定总结有些第三方库没有这样的问题,这与函数声明定义有关,具体可参考:TypeHintinginPyCharm

    2022年8月26日
    3
  • spring 注解验证@NotNull等使用方法

    spring 注解验证@NotNull等使用方法常用标签@Null被注释的元素必须为null@NotNull被注释的元素不能为null@AssertTrue被注释的元素必须为true@AssertFalse被注释的元素必须为false@Min(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值@Max(value)被注释的元素必须是一个数字,其值必须小于等于指定的最大值@DecimalMin(

    2022年6月13日
    35
  • clion 2021 激活_在线激活

    (clion 2021 激活)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/ide…

    2022年3月30日
    114
  • SpringBoot上传文件类型检测「建议收藏」

    SpringBoot上传文件类型检测「建议收藏」判断文件类型的三种方式1.通过文件后缀名这个方法只要修改后缀名就可以了2.通过Content-Type判断由于Content-Type取决于文件类型,文件类型取决于文件扩展名,所以改变了文件扩展名也就改变了Content-Type3.通过文件头判断文件即使文件扩展名改变了文件头也不会改变

    2022年6月11日
    335
  • redis 客户端连接及常用命令使用[通俗易懂]

    redis 客户端连接及常用命令使用[通俗易懂]一、本地客户端连接[root@localhostredis]#bin/redis-cli127.0.0.1:6379>authrootOK显示”ok”表示连接成功常用命令操作redis的数据结构:redis存储的是:key,value格式的数据,其中key都是字符串,value有5种不同的数据结构value的数据结构:字符串类型string哈希类型hash:map格式列表类型list:linkedlist格式。支持重复元素集合类型set:不允

    2022年5月10日
    53

发表回复

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

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