🔴 The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOG2026-02-18 17:20:18.300 ERROR 8842 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]] with root cause java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x82' for column 'name' at row 1

⚡ Quick Fix Works 80% of the time

Alter your MySQL database and table to use utf8mb4 character set.

ALTER DATABASE mydb CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; ALTER TABLE users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

🧠 Why this Happens

Tap to expand the deep technical explanation

The client sent a string containing a 4-byte UTF-8 character (like an emoji: 😂, which is `\xF0\x9F\x98\x82` in bytes). Your MySQL database or table is configured to use `utf8` (which only supports 3-byte characters). MySQL rejects the 4-byte character because it cannot store it.

The HITEC City Parking Spot Analogy:

Imagine a mailbox designed to hold standard letters (utf8). Someone tries to stuff a small parcel (emoji) into it. The mail carrier (MySQL) refuses to accept it because the mailbox physically cannot hold that size of item.

🔁 How to Reproduce Confirm this is your error

Create a MySQL table with `CHARACTER SET utf8`. Try to insert a string containing an emoji (e.g., `INSERT INTO users (name) VALUES ('Deva 😂');`). The insert will fail.

🛠️ Solutions (5 Ways to Fix)

Solution 1✓ Most common cause

Alter the database and table charset

👉 Use this as the permanent fix for MySQL databases.

Change the character set from `utf8` to `utf8mb4`, which supports full 4-byte UTF-8 characters including emojis.

ALTER DATABASE mydb CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; ALTER TABLE users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Solution 2

Specify utf8mb4 in the JDBC connection URL

👉 Use this to ensure the JDBC driver encodes strings correctly.

Append `characterEncoding=utf8mb4` to your `spring.datasource.url` so the driver knows to use 4-byte UTF-8.

spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8mb4
Solution 3

Use @Column(columnDefinition) in JPA

👉 Use this if you want Hibernate to create the table with the correct charset automatically.

Tell Hibernate to explicitly generate the column with `utf8mb4`.

@Entity public class User { @Column(columnDefinition = "VARCHAR(255) CHARACTER SET utf8mb4") private String name; }
Solution 4

Strip emojis before saving (Fallback)

👉 Use this if you cannot change the database schema.

If you don't control the DB, filter out 4-byte characters in Java before saving.

String cleanName = name.replaceAll("[^\\p{L}\\p{N}\\p{M}\\p{Z}\\p{P}\\p{S}]", ""); user.setName(cleanName);
Solution 5

Update MySQL configuration file (my.cnf)

👉 Use this to set utf8mb4 as the default for the entire MySQL server.

Prevent this issue for future tables by setting the server default.

# /etc/mysql/my.cnf [mysqld] character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci

📋 Version Notes

Spring Boot 2.x

Uses MySQL Connector/J 8.0.x.

Spring Boot 3.x

Uses MySQL Connector/J 8.0.x or 8.1.x.

🛡️ How to Prevent This Next Time

Always configure new MySQL databases to use `utf8mb4` by default. PostgreSQL supports 4-byte UTF-8 out of the box and does not have this specific issue.