SpringBoot配置项目字符编码
2019-03-10 本文已影响0人
永无止境_
一、传统Spring提供的字符编码过滤器
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean registration = new FilterRegistrationBean();
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setForceEncoding(true);
characterEncodingFilter.setEncoding("UTF-8");
registration.setFilter(characterEncodingFilter);
registration.addUrlPatterns("/*");
return registration;
}
在main方法上扫描此过滤器
注意:
application.properties要添加以下代码,否则上面代码不会生效
spring.http.encoding.enabled=false
二、在application.properties配置字符编码
spring.http.encoding.enabled=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.force=true