3

I have a Spring Boot (version 1.3) Web application, all in UTF-8, and a PostgreSQL server with server encoding UTF-8 and client encoding WIN1252.

The problem is that when I save in the DB something with special characters manually, the encoding is right, but from JDBC the string is saved wrong.

In the same way, when I have a string saved with special characters and I make a SELECT query to read manually, the encoding is right, but from JDBC the string is read wrongly.

Dropping the database and creating again is not a choice for the moment, so I have been trying a lot of things with poor result. Is there any solution for me?

PD: I have put all these properties in my application.properties file:

spring.datasource.sql-script-encoding = UTF-8
spring.http.encoding.charset = UTF-8
spring.http.encoding.enabled = true
spring.http.encoding.force = true
spring.mandatory-file-encoding = UTF-8
server.tomcat.uri-encoding = UTF-8
spring.messages.encoding = UTF-8
5
  • 1
    I think your property is incorrectly named. Try spring.datasource.sqlScriptEncoding=UTF-8 instead of spring.datasource.sql-script-encoding = UTF-8 Commented Mar 25, 2016 at 15:01
  • I'm using Spring Boot 1.3, I didn't say it sorry. The property is called as you said in Spring Boot 1.2 I think. Application Properties 1.3.3 Commented Mar 25, 2016 at 15:25
  • 1
    You are right, I'm sorry. Commented Mar 25, 2016 at 15:33
  • It works for me straight out of the box with the default Boot settings. I posted € over HTTP and it saved correctly in the db and returned € in the response. Which unicode chars are you trying? Commented Mar 25, 2016 at 20:58
  • I have just test it saving '€' character, and it has been encoded as 'Ç' character. Have you the same server and client encoding as me? Commented Mar 25, 2016 at 21:04

4 Answers 4

4

I had a similar problem. I use Spring Boot 1.5.9 and postgres 9.6, data.sql with inserts. Encoding on data.sql was UTF-8, but after start of application, in DB was special symbols instead russian letters.

spring.datasource.sql-script-encoding= UTF-8

This property in application.properties has helped.

All properties responsible for DB:

spring.datasource.jndi-name=java:/PostgresDS
spring.jpa.properties.hibernate.default_schema=my_scheme_name
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql = false
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.datasource.continue-on-error=true
spring.datasource.sql-script-encoding= UTF-8
Sign up to request clarification or add additional context in comments.

2 Comments

It is deprecated github.com/spring-projects/spring-boot/blob/v2.6.3/… and remove in Spring Boot 3.0.
@happy_marmoset it sounds great!
1

Now the correct option is:

spring.sql.init.encoding = UTF-8

Comments

0

Where you define the datasource URL, try adding ?characterEncoding=UTF-8 to the end of the URL

1 Comment

I added it, but the problem continues :(
0

In my case the problem was on JVM side.

After I added:

-Dfile.encoding=UTF8

as VM option - problem was resolved

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.