title: Spring4 @ResponseBody set default charset to utf8 with StringHttpMessageConverter
body: <p>I am trying to return plain text message using ajax and rest.</p>
  
  <p>Here is my controller for that:</p>
  
  <pre><code>@RequestMapping(value="/test", method=RequestMethod.PUT)
  @ResponseBody
  public String ajaxTest() {
      return "Characters test: ęółąśżźćń";
  }
  </code></pre>
  
  <p>But unfortunatelly I am getting:</p>
  
  <pre><code>  Characters test: ?�???????
  </code></pre>
  
  <p>I know that I have to make StringHttpMessageConverter to use UTF-8 charset as default.
  Even I have some xml configuration to it
  <a href="https://jira.spring.io/browse/SPR-9099" rel="nofollow">https://jira.spring.io/browse/SPR-9099</a></p>
  
  <pre><code>&lt;bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter"&gt;
    &lt;property name="supportedMediaTypes" value="text/plain;charset=UTF-8"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>but I use Java config instead of xml conf.</p>
  
  <p>Here is my initializer;</p>
  
  <pre><code>public class Initializer implements WebApplicationInitializer {
  
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
          ctx.register(WebAppConfig.class);
          ctx.register(ThymeleafConfig.class);
          servletContext.addListener(new ContextLoaderListener(ctx));
  
          ctx.setServletContext(servletContext);
  
          Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
          servlet.addMapping("/");
          servlet.setAsyncSupported(true);     
          servlet.setLoadOnStartup(1);
          // Allow to use Put and Delete method for REST architecture
          registerCharachterEncodingFilter(servletContext);
          registerHiddenFieldFilter(servletContext);
  
      }
  
  
      private void registerCharachterEncodingFilter(ServletContext aContext) {
          CharacterEncodingFilter cef = new CharacterEncodingFilter();
          cef.setForceEncoding(true);
          cef.setEncoding("UTF-8");
          aContext.addFilter("charachterEncodingFilter", cef).addMappingForUrlPatterns(null ,true, "/*");
      }
      private void registerHiddenFieldFilter(ServletContext aContext) {
          aContext.addFilter("hiddenHttpMethodFilter", new HiddenHttpMethodFilter()).addMappingForUrlPatterns(null ,true, "/*"); 
      }
      private StringHttpMessageConverter stringConverter(ServletContext aContext) {
          StringHttpMessageConverter shmc = new StringHttpMessageConverter();
          shmc.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "plain", Charset.forName("UTF-8"))));
          return shmc;
      }
  }
  </code></pre>
  
  <p>but I can make it work. Please help.</p>