Character difference enconding Charset Difference using JPA but not with Prepared statement

Asked

Viewed 86 times

1

I have an application using spring boot + oracle (via Tomcat datasource). Using spring-data, a simple query in a table returns encoding problem characters.

This same application, in a very specific situation, I create a direct connection (without datasource) with JDBC and run a query in the same table. But in this case, there is no problem with encoding.

Obs.: Before I thought it would just be a problem with the front-end (some JS component), but checking the HTTP Response by the browser, the data is already in problem in the first case, but not in the second.

I’m sorry if this is a duplicate question, but I couldn’t find anything similar. I know there are a lot of issues with oracle+java+charset, but my problem differs from the others I found.

  • Hello, Igor. You have set up Spring Boot or some filter in your application to force encoding UTF-8 in the application? Have any encoding settings in the URL your connection to the database? Can you debug the application and see if the data is already recovered with problems? Also check that the HTTP request has some encoding defined in the response. Unfortunately there are several variables that need to be analyzed. If you can check the above points and add more information to your question, we may help you more.

  • @utluiz, thank you so much for the answer. I didn’t set anything specific in Spring or the connection URL. As in an external client environment, I will not debug yet, but it will be my next attempt. As for HTTP, by checking by Sponse in the browser (F12 - Chrome), json already returns with error in requests via datasource and normal in requests via jdbc.

  • I put some encoding check tips in the answer because it is a common problem. If you find the cause, add an answer yourself.

1 answer

0

Identifying coding problems in a Java web application

There are several hypotheses that can only be validated with due verification in the system.

  1. The data is already corrupted in the database. In this case the problem is in receiving or saving the data.
    1. The server may be decoding the request incorrectly.
      • Check if the values already arrive corrupted in your controller, Servlet, action, etc..
      • Check whether the enctype form on the page sending the data is correct.
      • Make sure the browser sends the correct encoding in the request.
      • Check that the application server is configured with the appropriate encoding. Some application servers need encoding to be set before the request values are decoded, when you or the framework used first accesses these values. A common strategy is to use a Servlet filter.
    2. Data can be corrupted by being written to the database.
      • Check that the database table has the correct encoding. Databases like Mysql should use encoding by defaultutf8_general_ci or similar.
      • Check the documentation of driver of the database if in the settings of the connection to the database, such as the connection URL, already exists or it is necessary to add some configuration to define the encoding.
  2. The data is correct in the database, but it is corrupted in Java. In this case it is a bad configuration of the connection to the database.
    • Check the settings according to the previous item about connection to the bank.
  3. The data is read correctly in Java, but it gets corrupted in the client. In this case it may be that the browser is decoding the request response with a different encoding than the server used to write the response.
    • Check that the server sends the correct encoding in the response header. See the previous item about using a Servlet filter.
    • Make sure the server actually encodes the correct answer. See the previous section about using a Servlet filter.
    • Check for a tag <meta> defining a encoding other than the HTTP header.

Browser other questions tagged

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