Special character error in Java

Asked

Viewed 685 times

1

I’m having a problem here and I haven’t found anything that would solve on the internet, if you could help me, I would be extremely grateful.

Well, come on, I have a java web application that uses Spring Boot + JSP. The problem is this: I have two applications that share the same database, one of them records things and the other (web) reads, so far so good, only when I record something from the other application that is Desktop, anything that has an accent, in the database is correct but Java cannot read in the Web part and exchange for a strange character.

I’ve already tried:

  1. Create the environment variable: JAVA_TOOL_OPTIONS with the value = -Dfile.encoding=UTF8

  2. Add to file .properties from Spring to Line spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;

  3. Change the shape of the database URL: spring.datasource.url=jdbc:postgresql://localhost:5432/banco?useUnicode=true&characterEncoding=UTF-8

  4. I tried to configure in POM.

  5. I tried to create a filter.

  6. I tried to change the direct character by the application, however, as it arrives from the bank until my application wrong already, this did not help.

Important information:

The database is configured as follows:

ENCODING = 'SQL_ASCII';

LC_COLLATE = 'C';

LC_CTYPE = 'C'.

If I record something in the web part, I can read again with the accent normally, however, in the database is something like this:

Lixo

1 answer

3

First, make sure your Jsps include a <meta charset="UTF-8"> inside <head> of the HTML produced and before any other content of <head>.

Also, in connection with Postgresql, you use characterEncoding=UTF-8, but the database was configured with ENCODING = 'SQL_ASCII';. You should use ENCODING = 'UTF8';. As you quote the Postgresql documentation:

In Most cases, if you are Working with any non-ASCII data, it is unwise to use the SQL_ASCII Setting because Postgresql will be Unable to help you by Converting or validating non-ASCII characters.

Translating:

In most cases, if you are working with any non-ASCII data, it is not a good idea to use the SQL_ASCII configuration because Postgresql will not be able to help you when converting or validating non-ASCII characters.

In addition, in Postgresql, the encoding is set preferably at the time when the CREATE DATABASE is being executed.

  • Yes, my JSP’s have the right meta tag, I can display accent normally on them. The same problem is the arrival of the database to my application. In this case, in the BD is correct but, arrives in the wrong application. Regarding the characterEncoding=UTF-8, I put to see if it solved the problem, even without it, the problem persists. So you don’t have a way to reverse this without trading directly into the bank? Why the application in Delphi (Desktop) can manipulate the database normally using special character?

  • @Giovanioliveira The ideal would be to post in your question a minimum, complete and verifiable example (MVCE). That is, a complete code in Java and Postgresql settings that other people can copy and test to reproduce your problem and then search for solutions. However, this code would be minimal, that is, it would have only what would serve to demonstrate the problem and nothing more than that.

  • When you extract an MVCE code from the code of your program, you end up isolating the problem, which simplifies the search for the solution and allows other people to play it, and most of the time, you yourself end up finding the cause and the solution when trying to create the MVCE.

  • The problem is that I’m not doing anything too much, just giving a "statusDetalhe.getCodErro()", which in the bank is with accent but, in my application the message comes all messed up in place of accents. I have tried to display on the same console, without sending to JSP, with as little intervention as possible from Spring and still the problem occurs...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.