SpringBoot 设置编码UTF-8

第一种  通过过滤器来设置

@Configuration
public class UtfConfig {

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setForceEncoding(true);
        characterEncodingFilter.setEncoding("utf-8");
        registrationBean.setFilter(characterEncodingFilter);
        return registrationBean;
    }

}

main方法上面添加注解扫描此过滤器

然后在配置文件中添加如下配置:

# 根据过滤器配置字符编码 false 是将原来机制失效 过滤器配置的才生效#
spring.http.encoding.enabled=false

方法二 配置文件

spring.http.encoding.charset=utf-8
spring.http.encoding.force=true
spring.http.encoding.enabled=true

 

posted @ 2020-06-16 20:02  月半大熊猫  阅读(5584)  评论(0编辑  收藏  举报