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


相关推荐

  • navicat2021 7 mac激活码【2021免费激活】

    (navicat2021 7 mac激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlMLZPB5EL5Q-eyJsaWN…

    2022年3月20日
    46
  • 关于OpenProcessToken「建议收藏」

    关于OpenProcessToken「建议收藏」OpenProcessToken  要对一个任意进程(包括系统安全进程和服务进程)进行指定了写相关的访问权的OpenProcess操作,只要当前进程具有SeDeDebug权限就可以了。要是一个用户是Administrator或是被给予了相应的权限,就可以具有该权限。可是,就算我们用Administrator帐号对一个系统安全进程执行OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcessID)还是会遇到“访问拒绝”的错误。什么原因呢?原来在默认的情况下进程的一些访问权限

    2022年6月25日
    23
  • 《前端运维》一、Linux基础–03Shell基础及补充「建议收藏」

    诶诶欸?不是学Linux么?怎么要讲shell了?shell是啥?啥是shell?别急,我们先简单了解下shell是什么。Shell是一个用C语言编写的程序,它是用户使用Linux的桥梁。

    2022年3月25日
    38
  • 点击关闭当前页面

    点击关闭当前页面

    2022年3月1日
    78
  • 爬虫课第一次报错总结

    爬虫课第一次报错总结1importurllib.requesturl=’https://www.baidu.com/s?wd=海贼王’res=urllib.request.urlopen(url)UnicodeEncodeError:‘ascii’codeccan’tencodecharactersinposition10-12:ordinalnotinrange(128)原因:url包含不是ascii的字符处理:把“海贼王”改为%+十六进制(法1:百度,法2urllib.par

    2022年7月11日
    17
  • time_npl_timebeginperiod

    time_npl_timebeginperiod文章目录作用1准备1.1LunarSolarConverter.py1.2RangeTimeEnum.py1.3StringPreHandler.py1.4TimeNoemalizer.py1.5TimeUnit.py2测试3时间的正则表达式regex.txt信息来源Coconlp作用用于句子中时间词的抽取和转换res=tn.parse(target=u’过十分钟’)#target为待分析语句,timeBase为基准时间默认是当前时间print(res)

    2025年6月9日
    4

发表回复

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

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