Add a Servlet Filter in a Spring Boot application
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.
How to set up filter chain in spring boot?
Adding following line of code in GreetingFilter works
filterChain.doFilter(servletRequest, servletResponse);
Related Topics
What Should I Set Java_Home Environment Variable on MACos X 10.6
Differencebetween Set and List
How to Respond with an Http 400 Error in a Spring MVC @Responsebody Method Returning String
Make Maven to Copy Dependencies into Target/Lib
How to Make an Http Request Using Cookies on Android
Programmatically Getting the MAC of an Android Device
How to Print an Image on a Bluetooth Printer in Android
Using Mockito to Test Abstract Classes
Is It Good Practice to Use Java.Lang.String.Intern()
How to Bundle a Native Library and a Jni Library Inside a Jar
What's the C++ Idiom Equivalent to the Java Static Block
What Is Ld_Library_Path and How to Use It
Multiple Line Code Example in Javadoc Comment
How to Convert Currenttimemillis to a Date in Java
Casting Object Array to Integer Array Error
Convert an Array of Primitive Longs into a List of Longs