Spring 配置请求过滤器,编码格式设为UTF-8,避免中文乱码

时间:2023-11-26 08:33:44
 <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码-->
<filter>
<filter-name>springUtf8Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springUtf8Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

注解配置:

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

spring源码

 public class CharacterEncodingFilterextends OncePerRequestFilter {

     private String encoding;

     private boolean forceEncoding = false;

     /**
* Set the encoding to usefor requests. This encoding will be passed into a
* {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
* <p>Whether this encoding will overrideexisting request encodings
* (and whether it will beapplied as default response encoding as well)
* depends on the {@link #setForceEncoding "forceEncoding"} flag.
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
} /**
* Set whether theconfigured {@link #setEncoding encoding} of this filter
* is supposed to overrideexisting request and response encodings.
* <p>Default is "false", i.e. do notmodify the encoding if
* {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
* returns a non-null value.Switch this to "true" to enforce the specified
* encoding in any case,applying it as default response encoding as well.
* <p>Note that the response encoding will onlybe set on Servlet 2.4+
* containers, sinceServlet 2.3 did not provide a facility for setting
* a default responseencoding.
*/
public void setForceEncoding(boolean forceEncoding) {
this.forceEncoding = forceEncoding;
} @Override
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response,FilterChain filterChain)
throws ServletException, IOException { if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(this.encoding);
if (this.forceEncoding) {
response.setCharacterEncoding(this.encoding);
}
}
filterChain.doFilter(request, response);
}
}

该字符集过滤器有两个重要参数,分别是encodingforceEncoding

setEncoding

public void setEncoding(java.lang.String encoding)

Set the encodingto use for requests. This encoding will be passed into aServletRequest.setCharacterEncoding(java.lang.String) call.

setForceEncoding

public void setForceEncoding(boolean forceEncoding)

Set whether theconfigured encoding of this filter is supposed to override existing request andresponse encodings.

通过参考文档,我们可以知道:

l.第一个方法setEncoding()相当于:ServletRequest.setCharacterEncoding(java.lang.String),即设置request编码格式

2.第二个方法setForceEncoding()的作用是:同时设置ServletResponseServletRequest的编码格式。

配置相当于代码中:

resp.setCharacterEncoding("UTF-8");
req.setCharacterEncoding("UTF-8");

在请求处理的过程中我们可以不用考虑编码方面的问题,上面两句代码可以省略,编码统一交给Spring过滤器去处理。