1.方式一(使用传统的Spring提供的字符集过滤器)
先写一个Servlet。
package com.songzihao.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); } }
之后创建配置类 SystemConfig,在其中声明字符集过滤器的相关配置。
package com.songzihao.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 characterEncodingFilterRegistrationBean() { //创建字符集编码过滤器 CharacterEncodingFilter characterEncodingFilter=new CharacterEncodingFilter(); //设置强制使用指定的字符集编码 characterEncodingFilter.setForceEncoding(true); //设置指定的字符集编码 characterEncodingFilter.setEncoding("utf-8"); FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(); //设置字符集编码过滤器 filterRegistrationBean.setFilter(characterEncodingFilter); //设置字符集编码过滤器的路径 filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; } }
在SpringBoot核心配置文件中 关闭 SpringBoot 的 的 http 字符编码支持。
#关闭springboot的http字符编码支持 #只有关闭该选项之后,spring字符集编码过滤器才会生效 server.servlet.encoding.enabled=false
最后启动测试。
package com.songzihao.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan(basePackages = "com.songzihao.springboot.servlet") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
2.方式二(在 application.properties 中配置字符编码(推荐))
先写一个Servlet。
package com.songzihao.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); } }
在 SpringBoot 核心配置文件添加字符编码设置。
#设置请求响应的字符编码 server.servlet.encoding.enabled=true server.servlet.encoding.force=true server.servlet.encoding.charset=utf-8
最后启动测试。
package com.songzihao.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan(basePackages = "com.songzihao.springboot.servlet") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }