Igorovics Igorovics - 9 months ago 58
Java Question

Why UTF8 character encoding doesn't work properly in my project?

I'm developing a web application in Spring Framework under Wildfly 10 application server. I use Eclipse IDE.

I experienced problems with the character encoding which I wasn't able to solve: some of the characters doesn't appear properly when I send them from server side to client side.

My test

object I use is "
árvíztűrő tükörfúrógép

  1. When I read the value from database and send it to the client side, everything appears fine.

  2. When I set the String value in the Controller as a ModelAndView object, it appears as '??rv?­zt?±r?? t??k?¶rf??r??g?©p'

  3. When I send the value from client side by ajax as a POST variable and send it back to client side, it appears as 'árvízt?r? tükörfúrógép'.

I set all the .jsp files encoding UTF8:
<%@page contentType="text/html" pageEncoding="UTF-8"%>

In Eclipse I set all the Maven modules text file encoding to UTF8. All the files are in UTF8.

What did I miss? What else should I set to get the
value right on client side? Should I set the character encoding in Wildfly 10 somehow?

Could somebody help me? If you need further information, please don't hesitate to ask. Thank you.

EDIT: Setting the character encoding as Maven property solved the second case. Now I only have problems with the third case:


Answer Source

After nearly 2 months of searching I was able to find solution to my problem. In addition to configure the server and Spring, I needed to add two more things:

On the web module of my Maven project I had to set the character encoding of the source: <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

Also when I sent JSONObject to client side, I had to set the character encoding: @RequestMapping(value = "/historyContent.do", produces = { "application/json; charset=UTF-8" })

And finally I can see what I wanted.