使用spring-boot开发时候,有时候程序没事,往往不经意会造成中文到前端变成乱码(????这样情况)
下面给出spring-boot项目统一字符集设置方案:
1、Spring Boot修改编码方法,在application.properties里面配置
spring.http.encoding.charset=UTF-8
spring.http.encoding.force=true
spring.http.encoding.enabled=true
或者在Spring Boot 项目的application.yml里面配置
spring:
http:
encoding:
charset: UTF-8
force: true
enabled: true
2、在Bean中配置(启动类)
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setForceEncoding(true);
characterEncodingFilter.setEncoding("UTF-8");
registrationBean.setFilter(characterEncodingFilter);
return registrationBean;
}
PS:最后强调一点,开发中保持前后端以及DB字符集一致!