且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring Boot MVC错误编码的POST请求

更新时间:2021-07-15 05:01:18

深入研究许多过滤器链并在其中移动了字符编码后,终于找到了问题。

After digging into lots of filter chains and moving character encoding in them, finally found the problem. Another filter was changing encoding before Character encoding kick in.

我只是从spring security手中得到了过滤器,并将其作为bean手动添加,为我做过滤器:

I just get filter from spring security's hand and add it manually as a bean to do the filtering for me:

@Bean
public FilterRegistrationBean filterRegistrationBean() {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    registrationBean.setFilter(characterEncodingFilter);
    return registrationBean;
}

2天或搜索并尝试,只是一个简单的答案。找到答案的困难方式

2 days or searching and trying and just a simple answer. What a hard way to find an answer

---更新---

如果您使用的是Spring Boot 1.2+,则完全不需要CharacterEncoding。默认情况下,它处于理想位置,字符编码等于utf8。因此,下次,只要设置属性(如有)即可! 要设置的属性

If you are using spring boot 1.2+, there is no need for CharacterEncoding at all. it is in its perfect position by default with character encoding equal to utf8. So next time, just set attributes if there are any! the properties to set are:

# HTTP encoding (HttpEncodingProperties)
spring.http.encoding.charset=UTF-8 # the encoding of HTTP requests/responses
spring.http.encoding.enabled=true # enable http encoding support
spring.http.encoding.force=true # force the configured encoding

---更新2 ---

Tomcat Wiki :在 Tomcat 8 中从8.0.0(具体来说是8.0.0-RC3)开始,元素上的 URIEncoding 属性的默认值取决于严格的servlet遵从性设置。 URIEncoding的默认值(严格符合性已关闭)现在为 UTF-8 。如果启用了严格的servlet遵从性,则默认值为 ISO-8859-1

From Tomcat wiki: In Tomcat 8 starting with 8.0.0 (8.0.0-RC3, to be specific), the default value of URIEncoding attribute on the element depends on "strict servlet compliance" setting. The default value (strict compliance is off) of URIEncoding is now UTF-8. If "strict servlet compliance" is enabled, the default value is ISO-8859-1.

,则无需在Tomcat Config neigter中设置UriEncoding。

so generally saying, there is no need to set the UriEncoding in Tomcat Config neigter.