I am using a third party filter in my Spring Boot
3.x.x application. This particular filter implements the javax.servlet.Filter
.
import javax.servlet.Filter;
public class ThirdPartyFilter implements Filter { }
I created a FilterRegistrationBean
to register ThirdPartyFilter
as below:
@Configuration
public class FilterConfiguration {
@Bean
public FilterRegistrationBean<ThirdPartyFilter> thirdPartyFilter() {
FilterRegistrationBean<ThirdPartyFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new ThirdPartyFilter());
return registration;
}
}
However, the issue is that FilterRegistrationBean
extends the jakarta.servlet.Filter
which is different from the filter that ThirdPartyFilter
implements. It basically gives this error:
Type parameter
‘com.med.filter.ThirdPartyFilter’ is not
within its bound; should implement ‘jakarta.servlet.Filter’
What can be done (but not considering these options):
- Downgrading the
Spring Boot
version for my application from 3.x.x to 2.x.x works fine since theFilterRegistrationBean
in older versions extendsjavax.servlet.Filter
but I don’t want to change the version. - I don’t have the ability to modify/update the third party filter to implement
jakarta.servlet.Filter
instead ofjavax.servlet.Filter
.
What I tried:
-
I created a simple bean as below but it doesn’t seem to initialize or invoke the filter.
@Bean public ThirdPartyFilter thirdPartyFilter() { return new ThirdPartyFilter(); }
I’m not sure what other options are available. Any suggestions will be greatly appreciated.