RestAPI in Java with MySQL DB: issue on storing da

2019-09-06 16:22发布

问题:

I am building an app and I can't seem to handle the data that contain diacritics or other type of UTF-8 characters. In my case I'm in need of this chars: ă-Ă-â-Â-î-Î-ş-Ş-ţ-Ţ.

Now, first, there is a input on the front-end that gathers some info from the users, sends it via AngularJS http module to the API. The headers are all good, the data goes to the API all good. Then, on the server side, if I log the info, it outputs: ?-?-â-Â-î-Î-?-?-?-?, and of course, the same info is put in the db (checked with phpmyadmin).

The weird thing is, as my app pulls the data from the db to print it to my app, all goes well, it prints as it should be ă-Ă-â-Â-î-Î-ş-Ş-ţ-Ţ, until I restart the server, after which it prints as it is shown in the db.

I want to point out that it isn't the cache, I'm testing the app in cache-free environment. I even tried to access from different end-points just to be sure it is not from some local issue.

Anyone has any ideas ?

回答1:

I solved it myself: the problem was from JPA persistence level from Java. It didn't interpret the UTF-8 right because I was giving it inside the url. Actually what I needed to do was to create a parameter in glassfish jdbc.