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


相关推荐

  • springboot 注解_pagehelper分页原理

    springboot 注解_pagehelper分页原理案例环境jdk1.8,mysql8.0,idea(工具),springboot,mybatis-plus详情看pom.xml项目结构结构解释项目由网关,公共依赖模块,和商品的优惠券,商品服务,订单服务,仓储服务和会员服务组成(案例测试使用shop-product,common),父工程为聚合工程不作为依赖管理一、common模块(组件可在人人开源项目中获取renrne-fast),common是一个基础maven项目pom.xml(common)<?xm.

    2022年7月28日
    5
  • 微软第一台电脑_世界上第一位皇帝是谁

    微软第一台电脑_世界上第一位皇帝是谁2月9日,历史上的今天,世界上第一位计算机科学博士DavidWheeler出生;BAN逻辑的提出者RogerNeedham出生;人工智能控制论先驱KevinWarwick出生;谷歌发布GoogleBuzz;微软发布SurfacePro。

    2022年10月15日
    3
  • BZOJ1579 USACO 2009 Feb Gold 3.Revamping Trails Solution

    BZOJ1579 USACO 2009 Feb Gold 3.Revamping Trails Solution

    2022年1月11日
    60
  • 奥迪token失效,请重新登录_请求token失效怎么解决

    奥迪token失效,请重新登录_请求token失效怎么解决出现Token失效,请重新登录重新登录下就好了啊1首先我们的先拿到token2.放上Authorize再去测试自己的接口这样就好了

    2025年8月31日
    14
  • getParameterValues中文乱码[通俗易懂]

    getParameterValues中文乱码[通俗易懂]如果想获得一个元素的value情况时:可以设置为:Stringstr=newString(request.getParameter(“interest”).getBytes(“iso-8859-1”),“utf-8”);如果你获得的是得到复选框中选中的元素value值(有多个),只用在Servlet文件里添加request.setCharacterEncoding(“UTF-8”)…

    2022年7月22日
    21
  • Java异常处理习题

    Java异常处理习题一、选择题1、java中用来抛出异常的关键字是()A.try     B.catch   C.throw         D.finally 2、关于异常,下列说法正确的是()A.异常是一种对象       B.一旦程序运行,异常将被创建C.为了保证程序运行速度,要尽量避免异常控制     D.以上说法都不对 3、()类是所有异常类的父类。A.Th

    2022年5月12日
    76

发表回复

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

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