title: spring boot + thymeleaf cannot correctly encode UTF-8 characters form POST
body: <p>I am evaluating spring boot with thymeleaf template engine. I am facing the problem of UTF8 character form posting problem.</p>
  
  <p>For the simplest <code>form.html</code> post example :</p>
  
  <pre><code>&lt;head&gt;
    &lt;title&gt;test&lt;/title&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;form th:action="@{/test/doPost}" method="post"&gt;
    &lt;input type="text" name="name" /&gt;
    &lt;button type="submit"&gt;submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And in the controller :</p>
  
  <pre><code>@Controller
  @RequestMapping("/test")
  public class TestController {
  
    private Logger logger = LoggerFactory.getLogger(getClass());
  
    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public String showForm() {
      return "test/form";
    }
  
    @RequestMapping(value = "/doPost", method = RequestMethod.POST)
    public String doPost(@RequestBody String body,
                         @RequestParam(value = "name", required = true) String name) {
      logger.info("body = {}", body);
      logger.info("name = {}", name);
      return "redirect:/test/form";
    }
  }
  </code></pre>
  
  <p>Spring MVC (or thymeleaf) cannot correctly encode form with UTF-8 character in this example.</p>
  
  <p>If I input <code>ä</code> , I'll see this log :</p>
  
  <pre><code>TestController - body = name=%C3%83%C2%A4
  TestController - name = Ã¤
  </code></pre>
  
  <p><code>ä</code> should be encoded to <code>%C3%A4</code> . the body should be <code>name=%C3%A4</code> , but I don't know why it becomes <code>%C3%83%C2%A4</code> and decoded to wrong characters.</p>
  
  <p>I googled and found some solutions , such as adding an encoding filter :</p>
  
  <pre><code>  @Bean
    public Filter characterEncodingFilter() {
      CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
      characterEncodingFilter.setEncoding("UTF-8");
      characterEncodingFilter.setForceEncoding(true);
      return characterEncodingFilter;
    }
  </code></pre>
  
  <p>But not working.</p>
  
  <p>Another solution is to <code>thymeleafViewResolver.setCharacterEncoding("UTF-8");</code> by this way :</p>
  
  <pre><code>  @Bean
    public SpringResourceTemplateResolver templateResolver() {
      final SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
      templateResolver.setCacheable(false);
      templateResolver.setPrefix("classpath:/templates/");
      templateResolver.setSuffix(".html");
      templateResolver.setTemplateMode("HTML5");
      return templateResolver;
    }
  
    @Bean
    public SpringTemplateEngine springTemplateEngine() {
      SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
      springTemplateEngine.setTemplateResolver(templateResolver());
  
      return springTemplateEngine;
    }
  
    @Bean
    public ThymeleafViewResolver viewResolver() {
      ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
      thymeleafViewResolver.setTemplateEngine(springTemplateEngine());
      thymeleafViewResolver.setCharacterEncoding("UTF-8");
      return thymeleafViewResolver;
    }
  </code></pre>
  
  <p>But it doesn't work , either. </p>
  
  <p>Moreover , <code>ThymeleafViewResolver</code>'s characterEncoding is already set in the <a href="https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java#L211" rel="nofollow noreferrer">ThymeleafAutoConfiguration source code</a> . There seems no need to re-define <code>UTF-8</code> here.</p>
  
  <p>It seems I have similar situation with this question : <a href="https://stackoverflow.com/a/16064963/298430">UTF-8 encoding with form post and Spring Controller</a> . The accepted solution is </p>
  
  <blockquote>
    <p>Moving CharacterEncodingFilter to the top and forcing the encoding to
    be set as UTF-8 solved the problem.</p>
  </blockquote>
  
  <p>I tried this :</p>
  
  <pre><code>  @Bean
    public FilterRegistrationBean filterRegistrationBean() {
      FilterRegistrationBean registrationBean = new FilterRegistrationBean();
      CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
      characterEncodingFilter.setEncoding("UTF-8");
      characterEncodingFilter.setForceEncoding(true);
      registrationBean.setFilter(characterEncodingFilter);
      registrationBean.setOrder(0);
      return registrationBean;
    }
  </code></pre>
  
  <p>But still not working.</p>
  
  <p>I think UTF-8 form posting is very basic , but why it just cannot work here . Did I miss anything ?</p>
  
  <p>environment :</p>
  
  <pre><code>&lt;springboot.version&gt;1.3.0.M5&lt;/springboot.version&gt;
  &lt;spring.version&gt;4.2.1.RELEASE&lt;/spring.version&gt;
  </code></pre>
  
  <p>Thanks a lot.</p>
  
  <p><em>A little off-topic : I've used many other frameworks , such as JSP/wicket/grails/play1/play2 , non of these have such annoying UTF-8 situation.</em></p>