Spring Boot项目字符集乱码解决方案

今天打开自己做的商品操作后台,在操作的过程中发现添加的商品乱码。折腾了一番,最终解决了,记录下解决方案。

1、我在本地开发的时候,进行操作是不会乱码的,但是部署到服务器上就乱码了,这个时候应该排除是项目的乱码问题。

2、登录数据库,进行操作。发现服务器字符集有问题。

MariaDB [(none)]> show variables like 'character_set%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /opt/lampp/share/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

3、改变 character_set_server 与character_set_database字符集,修改mysql的配置文件在mysqld部分做如下修改

[mysqld]
character-set-server=utf8

4、重启数据库

MariaDB [(none)]> show variables like 'character_set%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /opt/lampp/share/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

附录

1、Spring Boot修改编码方法,在application.properties里面配置

spring.http.encoding.charset=UTF-8
spring.http.encoding.force=true
spring.http.encoding.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;
    }

3、参考一篇我之前写过的文章,那时候我写的好认真
http://amyhehe.blog.51cto.com/9406021/1698344

上一篇:精彩看点:2019云栖大会,阿里云MVP发生了哪些大事?


下一篇:php 理解