添加在Spring启动应用程序一个Servlet过滤器 [英] Add a Servlet Filter in a Spring Boot application
问题描述
我想有<一个href=\"http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-etag\">ETag询问服务。为此,有一个 ShallowEtagHeaderFilter
这所有的工作。如何添加它没有在我的 web.xml中声明它
(这实际上是不存在的,因为我不知何故没有了它迄今为止)?
P.S。我使用Spring 1.1.4启动
P.P.S。下面是一个完整的解决方案。
包cuenation.api;进口org.springframework.boot.context.embedded.FilterRegistrationBean;
进口org.springframework.context.annotation.Bean;
进口org.springframework.context.annotation.Configuration;
进口org.springframework.web.filter.ShallowEtagHeaderFilter;进口javax.servlet.DispatcherType;
进口java.util.EnumSet中;@组态
公共类WebConfig { @豆
公共FilterRegistrationBean shallowEtagHeaderFilter(){
FilterRegistrationBean注册=新FilterRegistrationBean();
registration.setFilter(新ShallowEtagHeaderFilter());
registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
registration.addUrlPatterns(/提示类别);
返回登记;
}}
当使用Spring启动
作为<一个href=\"http://docs.spring.io/spring-boot/docs/1.1.6.RELEASE/reference/html/boot-features-developing-web-applications.html#boot-features-embedded-container-servlets-and-filters\">mentioned在参考文档,唯一需要的步骤是声明滤波器作为一个Bean在配置类,这是它!
@Configuration
公共类WebConfig { @豆
公共过滤shallowEtagHeaderFilter(){
返回新ShallowEtagHeaderFilter();
}
}
使用Spring MVC的
您很可能已经延伸出的 WebApplicationInitializer
。如果没有,那么你应该从 web.xml中转换你的web应用的配置
文件到 WebApplicationInitializer
类。
如果您的上下文配置住在XML文件(S),你可以创建扩展一个类 AbstractDispatcherServletInitializer
- 如果使用配置类, AbstractAnnotationConfigDispatcherServletInitializer
是正确的选择。
在任何情况下,可以再加入过滤器注册:
@覆盖
保护过滤器[] getServletFilters(){
返回新的过滤[] {
新ShallowEtagHeaderFilter();
};
}
的<一个完整的例子href=\"http://docs.spring.io/spring/docs/4.1.1.RELEASE/spring-framework-reference/html/mvc.html#mvc-container-config\">$c$c-based Servlet容器初始化是在Spring参考文档使用。
I'd like to have ETag suport. For this purpose there is a ShallowEtagHeaderFilter
which does all the work. How can I add it without declaring it in my web.xml
(which actually does not exist, because I somehow got by without it so far)?
P.S. I use Spring Boot 1.1.4
P.P.S. Here's a full solution
package cuenation.api;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import javax.servlet.DispatcherType;
import java.util.EnumSet;
@Configuration
public class WebConfig {
@Bean
public FilterRegistrationBean shallowEtagHeaderFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new ShallowEtagHeaderFilter());
registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
registration.addUrlPatterns("/cue-categories");
return registration;
}
}
When using Spring Boot
As mentioned in the reference documentation, the only step needed is to declare that filter as a Bean in a configuration class, that's it!
@Configuration
public class WebConfig {
@Bean
public Filter shallowEtagHeaderFilter() {
return new ShallowEtagHeaderFilter();
}
}
When using Spring MVC
You're probably already extending a WebApplicationInitializer
. If not, then you should convert your webapp configuration from a web.xml
file to a WebApplicationInitializer
class.
If your context configuration lives in XML file(s), you can create a class that extends AbstractDispatcherServletInitializer
- if using configuration classes, AbstractAnnotationConfigDispatcherServletInitializer
is the proper choice.
In any case, you can then add Filter registration:
@Override
protected Filter[] getServletFilters() {
return new Filter[] {
new ShallowEtagHeaderFilter();
};
}
Full examples of code-based Servlet container initialization are available in the Spring reference documentation.
这篇关于添加在Spring启动应用程序一个Servlet过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!