Spring Boot应用启动期间的Servlet错误

时间:2018-08-31 18:42:57

标签: spring-boot servlets

我正在尝试运行Spring Boot Web应用程序。

应用程序类如下:

@SpringBootApplication
public class MyWebApplication extends SpringBootServletInitializer {

  public static void main(String[] args) {
    System.setProperty("spring.profiles.active", "dev");
    SpringApplication.run(MyWebApplication.class, args);
  }


  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(MyWebApplication.class);
  }

  @Configuration
  @ConditionalOnWebApplication
  public static class WebConfiguration {

     @Bean
     public ServletListenerRegistrationBean<ServletContextListener> registerClientCookieConfigListener () {
        ServletListenerRegistrationBean<ServletContextListener> srb =
                new ServletListenerRegistrationBean<>();
        srb.setListener(new MyCookieConfigListener());
        return srb;
     }

    // ... other servlets and filters registered by ServletRegistrationBean or FilterRegistrationBean

  }
}

MyCookieConfigListener内部:

@WebListener
public class MyCookieConfigListener implements ServletContextListener {


  @Override
  public void contextInitialized(ServletContextEvent sce) {
    final SessionCookieConfig cookieConfig = sce.getServletContext().getSessionCookieConfig();
    cookieConfig.setHttpOnly(true);
    cookieConfig.setMaxAge(-1);
  }

  @Override
  public void contextDestroyed(ServletContextEvent sce) {

  }
 }

MyCookieListener也在web.xml中定义

启动应用程序时:

java.lang.UnsupportedOperationException: Section 4.4 of the Servlet 3.0 specification does not permit this method to be called from a ServletContextListener that was not defined in web.xml, a web-fragment.xml file nor annotated with @WebListener
    at org.apache.catalina.core.StandardContext$NoPluggabilityServletContext.getSessionCookieConfig(StandardContext.java:6665)
    at MyCookieConfigListener.contextInitialized(MyCookieConfigListener.java:20)

这发生在这一行:

     final SessionCookieConfig scc=sce.getServletContext().getSessionCookieConfig();

在web.xml中声明了侦听器,它也被注释为@Listener。 有没有人遇到这个问题并知道解决方案?

0 个答案:

没有答案