Spring Boot: Save arabic characters in mysql.

A few weeks ago, I had to scrape a website, get some data from it and put it into a mysql Database. This data should be exposed also with rest endpoints. This was a small app, and of course I chosen to work with my favorite Java framework Spring boot!

Below Steps that describe how I achieved this. Most of you probably know this, but I'm sharing it maybe someone will find it useful.

Step 1

Set the default character set and collate of your database to utf8 and utf8generalci consecutively.

ALTER DATABASE `dbname` CHARACTER SET utf8 COLLATE utf8_general_ci;

Step 2

Set the default character set of your table to ‘UTF8’ as well

ALTER TABLE `table_name` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci

Step 3

Copy the following configuration to you application.yml file

      spring:
        datasource:
          url: jdbc:mysql://<IP>:<PORT>/<DB_NAME>?useSSL=false&useUnicode=yes&characterEncoding=UTF-8&characterSetResults=UTF-8
          username: <USERNAME>
          password: <PASSWORD>
        jpa:
          show-sql: true
          hibernate:
            ddl-auto: update
            naming:
              strategy: org.hibernate.cfg.ImprovedNamingStrategy
          properties:
            hibernate:
              dialect: org.hibernate.dialect.MySQL5Dialect
              connection:
                characterEncoding: utf-8
                CharSet: utf-8
                useUnicode: true
        jackson:
          serialization:
            INDENT_OUTPUT: true
        http:
            encoding:
              charset: UTF-8
              enabled: true
              force: true

And voila! That's all ;)