title: Response encoding in Spring
body: <p>
  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:
  </p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST, value = "dispatcher")
  @ResponseBody
  public String dispatcherPost(HttpServletRequest req, HttpServletResponse resp, HttpSession session) {
      return process(req, resp, session);
  }
  </code></pre>
  
  <p>
  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: 
  <p>`resp.setContentType("application/json;charset=UTF-8");`</p>
  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
  </p>
  
  <pre><code>&lt;!-- Force Char Encoding --&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;encoding&lt;/param-name&gt;
          &lt;param-value&gt;UTF-8&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
   &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>
  So basically the problem is with returning well encoded JSON from server. Any idea?
  </p>
  
  <p>EDIT:</p>
  
  <p>I paste code into proccess method:</p>
  
  <p>
  
      System.err.println("TEST " + System.getProperty("file.encoding"));<br>
      System.err.println("TEST normal: " + response);<br>
      System.err.println("TEST cp1250: " + new String(response.getBytes(),"cp1250"));<br>
      System.err.println("TEST UTF-8: " + new String(response.getBytes(),"UTF-8"));<br>
  
  </p>
  
  <p>
  And result is something like this:
  
  
  
  <p>TEST Cp1250<br>
  TEST normal: {"users":[{"login":"userąęśćółżń"}]}<br>
  TEST cp1250: {"users":[{"login":"userąęśćółżń"}]}<br>
  TEST UTF-8: {"users":[{"login":"user?????"}]}<br>
  </p>
  
  </p>
  
  <p>
  Thanks, Arek
  </p>