spring boot + thymeleaf 无法正确编码 UTF-8 字符形式 POST

spring boot + thymeleaf cannot correctly encode UTF-8 characters form POST

我正在评估 spring 使用 thymeleaf 模板引擎启动。我正面临 UTF8 字符形式的问题 posting 问题。

对于最简单的 form.html post 示例:

<head>
  <title>test</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>

<body>

<form th:action="@{/test/doPost}" method="post">
  <input type="text" name="name" />
  <button type="submit">submit</button>
</form>

在控制器中:

@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";
  }
}

Spring 在此示例中,MVC(或 thymeleaf)无法使用 UTF-8 字符正确编码表单。

如果我输入 ä ,我会看到这个日志:

TestController - body = name=%C3%83%C2%A4
TestController - name = ä

ä 应编码为 %C3%A4 。正文应该是 name=%C3%A4 ,但我不知道为什么它会变成 %C3%83%C2%A4 并解码为错误的字符。

我用谷歌搜索并找到了一些解决方案,例如添加编码过滤器:

  @Bean
  public Filter characterEncodingFilter() {
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceEncoding(true);
    return characterEncodingFilter;
  }

但不工作。

另一种解决方案是 thymeleafViewResolver.setCharacterEncoding("UTF-8"); 通过这种方式:

  @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;
  }

但它也不起作用。

此外,ThymeleafViewResolver的字符编码已经在ThymeleafAutoConfiguration source code中设置。这里好像不需要重新定义UTF-8

看来我和这个问题有类似的情况:UTF-8 encoding with form post and Spring Controller。接受的解决方案是

Moving CharacterEncodingFilter to the top and forcing the encoding to be set as UTF-8 solved the problem.

我试过了:

  @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;
  }

但还是不行。

我认为 UTF-8 格式 posting 是非常基本的,但为什么它不能在这里工作。我错过了什么吗?

环境:

<springboot.version>1.3.0.M5</springboot.version>
<spring.version>4.2.1.RELEASE</spring.version>

非常感谢。

有点跑题:我用过很多其他的框架,比如JSP/wicket/grails/play1/play2,没有一个有这么烦人的UTF-8情况。

This is a known issue, already fixed - 修复程序将随 Spring Boot 1.3.0.RC1.

一起提供

同时,您可以通过手动声明编码过滤器来解决此问题:

@Autowired
private HttpEncodingProperties httpEncodingProperties;

@Bean
public OrderedCharacterEncodingFilter characterEncodingFilter() {
    OrderedCharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
    filter.setEncoding(this.httpEncodingProperties.getCharset().name());
    filter.setForceEncoding(this.httpEncodingProperties.isForce());
    filter.setOrder(Ordered.HIGHEST_PRECEDENCE);
    return filter;
}