CharacterEncodingFilter

CharacterEncodingFilter

本文介绍了如何在 SpringBoot 中配置 CharacterEncodingFilter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习Spring Boot时遇到了一些编码问题;我想添加一个像 Spring 3.x 这样的 CharacterEncodingFilter.就像这样:

I encountered some encoding problems in learning Spring Boot;I want to add a CharacterEncodingFilter like Spring 3.x.just like this:

<filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

推荐答案

Application.java 类的示例代码,如以上评论中所建议的:

Example code for your Application.java class, as proposed in the comments above:

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

这篇关于如何在 SpringBoot 中配置 CharacterEncodingFilter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 20:12