Wrong encoding between Spring and PostgreSQL

烈酒焚心 提交于 2019-12-24 16:46:03

问题


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

回答1:


I was having the same problem. I'm using 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



回答2:


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



来源:https://stackoverflow.com/questions/36222213/wrong-encoding-between-spring-and-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!