Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a RequestContextListener with no-xml configuration?

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?

like image 858
Bogdan Timofeev Avatar asked Jul 05 '16 11:07

Bogdan Timofeev


2 Answers

I created this class and that solved my issue.

@Configuration
@WebListener
public class MyRequestContextListener extends RequestContextListener {
}
like image 68
Bogdan Timofeev Avatar answered Nov 30 '22 22:11

Bogdan Timofeev


Write your own listener class which extends from RequestContextListener and register it via annotation. Something like this:

@WebListener
public class MyRequestContextListener extends RequestContextListener {
}
like image 33
olexd Avatar answered Nov 30 '22 23:11

olexd