Spring boot JPA broken encoding on database when loaded from data.sql file

As covered in a previous article (here and here) spring-boot is a very useful framework when working on full enabled Spring projects

Recently I wasted some time with yet another encoding problem.. where all my accentuated and special characters where corrupted.

I was using the data.sql convention that states that if a file named data.sql is found in the CLASSPATH spring will use this file to bootstrap the database, my SQL file was properly encoded in UTF-8 as well as my maven-resources-plugin however the data recovered from the database was corrupted (accents and special characters where broken)

After reading a bit the documentation I found out that the source of the problem was at the moment of reading the SQL file that the encoding was causing trouble.. thankfully you can fix this one pretty easlily by changing one property value :

spring.datasource.sqlScriptEncoding=UTF-8

spring-boot has a lot of useful properties that allow you to tune the framework which you can find here

5 comments:

  1. Thanks you very much, man! I tried to fix this in hours and can't find any solution why my constraints about field length are broken.

    My problem was: org.h2.jdbc.jdbcsqlexception value too long for column

    I ever didn't know that value too long because of wrong encoding. You saved my life! Thanks!

    ReplyDelete
  2. Hey thanks for the feedback the error annoyed me for a while as well!

    ReplyDelete
  3. Thank you,
    was googling for "springboot data.sql encoding"

    ReplyDelete
  4. You saved my day. It works perfectly.

    ReplyDelete