1 SpringBoot设置字符集编码
SpringBoot设置字符集编码有两种方式
方式一:使用传统的 Spring 提供的字符编码过滤器
方式二:在 application.properties 中配置字符编码(推荐)
2??方式一:使用传统的 Spring 提供的字符编码过滤器
先写个servlet
package com.liuhaiyang.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("欢迎使用springboot框架,美丽的世界啊");
//设置统一的浏览器编码格式
resp.setContentType("text/html;character=utf-8");
resp.getWriter().flush();
resp.getWriter().close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
创建配置类 SystemConfig,在其中声明字符集过滤器的相关配置。
package com.liuhaiyang.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;
}
}
在SpringBoot核心配置文件中?关闭 SpringBoot 的 的 http 字符编码支持。
#关闭springboot的http字符集编码支持
#只有关闭了该选项后,spring字符集编码过滤器才能成效
server.servlet.encoding.enabled=false
启动类
package com.liuhaiyang.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan(basePackages = "com.liuhaiyang.springboot.servlet")
public class SpringbootTest15Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootTest15Application.class, args);
}
}
结果截图:
?
?3?方式二:在 application.properties 中配置字符编码(推荐)
?创建一个servlet类。
package com.liuhaiyang.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("欢迎使用springboot框架,美丽的世界啊,第二种方式设置字符集编码");
//设置统一的浏览器编码格式
resp.setContentType("text/html;character=utf-8");
resp.getWriter().flush();
resp.getWriter().close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
SpringBoot 核心配置文件添加字符编码设置。
#第二种方式,直接在核心配置文件上操作
#设置请求响应字符编码
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
server.servlet.encoding.charset=utf-8
启动测试(同样需要指定servlet类的位置,方法同上的启动类)
?
|