• SpringBoot框架下——设置字符编码——2种实现方式


    SpringBoot框架下设置字符编码

    1. 第一种方式: CharacterEncodingFilter

    1.1 创建Servlet类

    src/main/java/com/guo/springboot/servlet/MyServlet.java

    package com.guo.springboot.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet(urlPatterns = "/myservlet")     
    public class MyServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().println("世界你好! 点赞点赞 Hello World!");
            resp.setContentType("text/html;character=utf-8");
            //统一设置浏览器编码
            resp.getWriter().flush();
            resp.getWriter().close();
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req,resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    1.2 创建配置类:CharacterEncodingFilter

    src/main/java/com/guo/springboot/config/SystemConfig.java

    package com.guo.springboot.config;
    
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.filter.CharacterEncodingFilter;
    
    @Configuration   //声明此类为配置类
    public class SystemConfig {
        @Bean
        public FilterRegistrationBean myFilterRegistrationBean(){
            //创建字符编码过滤器
            CharacterEncodingFilter characterEncodingFilter=new CharacterEncodingFilter();
            //设置强制使用指定字符编码
            characterEncodingFilter.setForceEncoding(true);
            //设置指定字符编码
            characterEncodingFilter.setEncoding("utf-8");
    
            FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
            //设置字符编码过滤器
            filterRegistrationBean.setFilter(characterEncodingFilter);
            //设置字符编码过滤器路径
            filterRegistrationBean.addUrlPatterns("/*");
            return filterRegistrationBean;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    1.3 核心配置文件

    src/main/resources/application.properties

    #关闭SpringBoot的HTTP字符编码支持
    #只有关闭该选项后,spring字符编码过滤器才能生效
    server.servlet.encoding.enabled=false
    
    • 1
    • 2
    • 3

    1.4 程序入口类——开启包扫描

    src/main/java/com/guo/springboot/Application.java

    package com.guo.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    @SpringBootApplication
    @ServletComponentScan(basePackages = "com.guo.springboot.servlet")
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1.5 启动看结果

    在这里插入图片描述

    2. 第二种方式:SpringBoot字符编码(推荐)

    2.1 创建Servlet类

    src/main/java/com/guo/springboot/servlet/MyServlet.java

    package com.guo.springboot.servlet;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    @WebServlet(urlPatterns = "/myservlet")
    public class MyServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().println("世界你好! 点点赞 Hello World!");
            resp.setContentType("text/html;character=utf-8");
            //统一设置浏览器编码
            resp.getWriter().flush();
            resp.getWriter().close();
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req,resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2.2 核心配置文件

    src/main/resources/application.properties

    #设置请求响应字符编码
    #即使我们配置文件中不配置server.servlet.encoding.enabled=true,也是默认生效的。
    #让系统的CharacterEncdoingFilter生效
    server.servlet.encoding.enabled=true
    #强制request,response都使用charset属性的值
    server.servlet.encoding.force=true
    #server.servlet.encoding.charset=utf-8
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    之前的方法过时了,Alt+Enter自定提示转换新版的
    在这里插入图片描述

    2.3 程序入口类——开启包扫描

    src/main/java/com/guo/springboot/Application.java

    package com.guo.springboot;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    @SpringBootApplication
    @ServletComponentScan(basePackages = "com.guo.springboot.servlet")
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.4 启动看结果

    在这里插入图片描述

    两种方式都能达到相同的效果,显然第二种方式更为简单,方便。

  • 相关阅读:
    数据结构与算法_part_8_二叉树
    小程序自定义tabbar,中间凸起
    如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着。与空格子相邻的格子中的卡片可以移动到空格中。
    Redis02:企业架构介绍以及redis介绍
    python函数
    Bootstrap的咖啡网站实例代码阅读笔记
    gitlab安装
    程序员的技术VS业务,哪个更重要?
    玩转SpringBoot之定时任务@Scheduled线程池配置
    Windows系统编译libhv带SSL,开启WITH_OPENSSL
  • 原文地址:https://blog.csdn.net/qq_45896330/article/details/126187346