I need to add a listener to my Spring Boot application, in web.xml it looks like
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
I use no-web.xml configuration, so I've got a class like
public class AppFilterConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF8");
filter.setForceEncoding(true);
Filter[] filters = new Filter[1];
filters[0] = filter;
return filters;
}
private int maxUploadSizeInMb = 5 * 1024 * 1024; // 5 MB
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected void registerDispatcherServlet(ServletContext servletContext) {
super.registerDispatcherServlet(servletContext);
servletContext.addListener(new HttpSessionEventPublisher());
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addListener(new RequestContextListener());
}
}
As seen from code above, I have added a listener to onStartup(ServletContext servletContext) method, but it doesn't help as I still get
In this case, use RequestContextListener or RequestContextFilter to expose the current request.
this message. How can I properly add a listener to my Spring Boot Application?
I created this class and that solved my issue.
@Configuration
@WebListener
public class MyRequestContextListener extends RequestContextListener {
}
Write your own listener class which extends from RequestContextListener
and register it via annotation. Something like this:
@WebListener
public class MyRequestContextListener extends RequestContextListener {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With