I've got problem with character encoding of my http responses. I read many tips, tutorials etc., but I can't resolve my problem. We are using Spring MVC with Hibernate and ExtJS as view technology. All data are returns as JSON using @ResponseBody on controllers method. Example method:
@RequestMapping(method = RequestMethod.POST, value = "dispatcher")
@ResponseBody
public String dispatcherPost(HttpServletRequest req, HttpServletResponse resp, HttpSession session) {
return process(req, resp, session);
}
There is simple dispatching mechanism for dispatching url commands (not really important in this case). Process method is doing something with parameters and return JSON. This JSON contains for example data from database (PostgreSQL 9.1.4). Data in Postgres are stored in UTF-8 and are 'visible correctly' for example in pgAdmin. We can also see valid data (from database) while debuging in eclipse. It all looks like there is everything ok with getting data from the Postgres. Problem starts when we want to return them via @ResponseBody annotated method. Method 'process' returns valid string (I can see in debugging mode) with utf-8 characters but in web browser (chrome, firefox) there are '?' instead of polish characters which are stored in database. Looking into firebug I can see that response headers are partially valid: 'Content-type: text/html;charset=UTF-8'. I told 'partially', because I have this line of code in process method:
`resp.setContentType("application/json;charset=UTF-8");`
where resp is HttpServletResponse. I've tried adding springs bean post processor from this solution: http://stackoverflow.com/questions/3616359/who-sets-response-content-type-in-spring-mvc-responsebody/3617594#3617594 but it doesn't works. I've got also character encoding filter in web.xml<!-- Force Char Encoding -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
So basically the problem is with returning well encoded JSON from server. Any idea?
EDIT:
I paste code into proccess method:
System.err.println("TEST " + System.getProperty("file.encoding"));
System.err.println("TEST normal: " + response);
System.err.println("TEST cp1250: " + new String(response.getBytes(),"cp1250"));
System.err.println("TEST UTF-8: " + new String(response.getBytes(),"UTF-8"));
And result is something like this:
TEST Cp1250
TEST normal: {"users":[{"login":"userąęśćółżń"}]}
TEST cp1250: {"users":[{"login":"userąęśćółżń"}]}
TEST UTF-8: {"users":[{"login":"user?????"}]}
Thanks, Arek