Spring Boot war file on tomcat: errorPageFilter ca

2020-04-12 00:33发布

问题:

I'm trying to convert a simple Spring Boot app into a war file for deployment to my tomcat server but I keep on getting this error:

Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'errorPageFilter': Initialization of bean failed; nested exception is java.lang.ClassCastException: org.springframework.boot.context.web.ErrorPageFilter cannot be cast to org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory

This Spring Boot app works perfectly when I run it as a jar but if I create a war, it gives me errors. As far as I can tell, I followed the Spring guide for this perfectly: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file

This is my application class:

@Configuration
@EnableWebMvc
@ComponentScan({"XXX"})
@EnableAutoConfiguration
@EnableConfigurationProperties
@SpringBootApplication
@EnableAsync
public class Application extends SpringBootServletInitializer {

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

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

This is my build.gradle:

buildscript {
    ext {
        springBootVersion = '1.2.6.RELEASE'
    }
    repositories {
        maven { url "http://repo.spring.io/libs-release" }
        maven { url "http://repo.spring.io/libs-snapshot" }
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'application'

war {
    archiveName 'hello-gradle.war'
}

configurations {
    providedRuntime
}

compileJava {
    targetCompatibility = 1.8
    sourceCompatibility = 1.8
}

mainClassName = "XXX.Application"

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-release" }
    maven { url "http://repo.spring.io/libs-snapshot" }
    maven { url "http://maven.springframework.org/milestone" }
}

dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.fasterxml.jackson.core:jackson-databind:2.3.2'
    compile 'com.fasterxml.jackson.core:jackson-core:2.5.3'

    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-tomcat'
    compile 'org.springframework.boot:spring-boot-starter-security'
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-starter-aop'
    compile 'org.mongodb:mongo-java-driver:3.1.0'
    compile 'org.springframework.data:spring-data-mongodb:1.8.0.RELEASE'
    compile("org.springframework:spring-context-support")

    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.8'
}

This is my catalina.log:

> 12-Dec-2015 14:53:54.921 INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployWAR Deployment of web
> application archive C:\Tomcat8\webapps\hello-gradle.war has finished
> in 4,900 ms 12-Dec-2015 14:53:54.921 INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployDirectory Deploying web
> application directory C:\Tomcat8\webapps\docs 12-Dec-2015 14:53:54.993
> INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployDirectory Deployment of
> web application directory C:\Tomcat8\webapps\docs has finished in 72
> ms 12-Dec-2015 14:53:54.993 INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployDirectory Deploying web
> application directory C:\Tomcat8\webapps\examples 12-Dec-2015
> 14:53:55.239 INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployDirectory Deployment of
> web application directory C:\Tomcat8\webapps\examples has finished in
> 246 ms 12-Dec-2015 14:53:55.239 INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployDirectory Deploying web
> application directory C:\Tomcat8\webapps\host-manager 12-Dec-2015
> 14:53:55.262 INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployDirectory Deployment of
> web application directory C:\Tomcat8\webapps\host-manager has finished
> in 23 ms 12-Dec-2015 14:53:55.262 INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployDirectory Deploying web
> application directory C:\Tomcat8\webapps\manager 12-Dec-2015
> 14:53:55.283 INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployDirectory Deployment of
> web application directory C:\Tomcat8\webapps\manager has finished in
> 21 ms 12-Dec-2015 14:53:55.284 INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployDirectory Deploying web
> application directory C:\Tomcat8\webapps\ROOT 12-Dec-2015 14:53:55.316
> INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployDirectory Deployment of
> web application directory C:\Tomcat8\webapps\ROOT has finished in 32
> ms 12-Dec-2015 14:53:55.328 INFO [main]
> org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler
> ["http-apr-8080"] 12-Dec-2015 14:53:55.337 INFO [main]
> org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler
> ["ajp-apr-8009"] 12-Dec-2015 14:53:55.340 INFO [main]
> org.apache.catalina.startup.Catalina.start Server startup in 5354 ms
> 12-Dec-2015 14:54:25.834 INFO
> [ContainerBackgroundProcessor[StandardEngine[Catalina]]]
> org.apache.catalina.startup.HostConfig.undeploy Undeploying context
> [/hello-gradle] 12-Dec-2015 14:54:25.847 SEVERE
> [ContainerBackgroundProcessor[StandardEngine[Catalina]]]
> org.apache.catalina.startup.ExpandWar.deleteDir
> [C:\Tomcat8\webapps\hello-gradle\WEB-INF\lib] could not be completely
> deleted. The presence of the remaining files may cause problems
> 12-Dec-2015 14:54:25.848 SEVERE
> [ContainerBackgroundProcessor[StandardEngine[Catalina]]]
> org.apache.catalina.startup.ExpandWar.deleteDir
> [C:\Tomcat8\webapps\hello-gradle\WEB-INF] could not be completely
> deleted. The presence of the remaining files may cause problems
> 12-Dec-2015 14:54:25.848 SEVERE
> [ContainerBackgroundProcessor[StandardEngine[Catalina]]]
> org.apache.catalina.startup.ExpandWar.deleteDir
> [C:\Tomcat8\webapps\hello-gradle] could not be completely deleted. The
> presence of the remaining files may cause problems 12-Dec-2015
> 14:54:25.849 SEVERE
> [ContainerBackgroundProcessor[StandardEngine[Catalina]]]
> org.apache.catalina.startup.ExpandWar.delete
> [C:\Tomcat8\webapps\hello-gradle] could not be completely deleted. The
> presence of the remaining files may cause problems 12-Dec-2015
> 14:54:25.851 INFO [localhost-startStop-2]
> org.apache.catalina.startup.HostConfig.deployDirectory Deploying web
> application directory C:\Tomcat8\webapps\hello-gradle 12-Dec-2015
> 14:54:27.196 INFO [localhost-startStop-2]
> org.apache.catalina.startup.HostConfig.deployDirectory Deployment of
> web application directory C:\Tomcat8\webapps\hello-gradle has finished
> in 1,345 ms 12-Dec-2015 14:54:35.176 INFO [http-apr-8080-exec-12]
> org.apache.catalina.startup.HostConfig.undeploy Undeploying context
> [/hello-gradle] 12-Dec-2015 14:54:35.203 INFO [http-apr-8080-exec-12]
> org.apache.catalina.startup.HostConfig.deployWAR Deploying web
> application archive C:\Tomcat8\webapps\hello-gradle.war 12-Dec-2015
> 14:54:39.295 SEVERE [http-apr-8080-exec-12]
> org.apache.catalina.core.ContainerBase.addChildInternal
> ContainerBase.addChild: start:  
> org.apache.catalina.LifecycleException: Failed to start component
> [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/hello-gradle]]
>   at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
>   at
> org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
>   at
> org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
>   at
> org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
>   at
> org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945)
>   at
> org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:466)
>   at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1583)
>   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:497)     at
> org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
>   at
> com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
>   at
> com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
>   at
> org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1460)
>   at
> org.apache.catalina.manager.HTMLManagerServlet.upload(HTMLManagerServlet.java:288)
>   at
> org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:209)
>   at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)     at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:729)  at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
>   at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>   at
> org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:136)
>   at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
>   at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>   at
> org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
>   at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
>   at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>   at
> org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
>   at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
>   at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>   at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
>   at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
>   at
> org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614)
>   at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
>   at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
>   at
> org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
>   at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
>   at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
>   at
> org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
>   at
> org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
>   at
> org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
>   at
> org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
>   at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
>   at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
>   at
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
>   at java.lang.Thread.run(Thread.java:745) Caused by:
> org.springframework.context.ApplicationContextException: Unable to
> start embedded container; nested exception is
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'errorPageFilter': Initialization of bean
> failed; nested exception is java.lang.ClassCastException:
> org.springframework.boot.context.web.ErrorPageFilter cannot be cast to
> org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
>   at
> org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
>   at
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
>   at
> org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
>   at
> org.springframework.boot.SpringApplication.refresh(SpringApplication.java:687)
>   at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
>   at
> org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:133)
>   at
> org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:124)
>   at
> org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:81)
>   at
> org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
>   at
> org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5244)
>   at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>   ... 45 more Caused by:
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'errorPageFilter': Initialization of bean
> failed; nested exception is java.lang.ClassCastException:
> org.springframework.boot.context.web.ErrorPageFilter cannot be cast to
> org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
>   at
> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
>   at
> org.springframework.boot.context.embedded.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:209)
>   at
> org.springframework.boot.context.embedded.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:165)
>   at
> org.springframework.boot.context.embedded.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:160)
>   at
> org.springframework.boot.context.embedded.ServletContextInitializerBeans.addAdaptableBeans(ServletContextInitializerBeans.java:143)
>   at
> org.springframework.boot.context.embedded.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:74)
>   at
> org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getServletContextInitializerBeans(EmbeddedWebApplicationContext.java:234)
>   at
> org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.selfInitialize(EmbeddedWebApplicationContext.java:221)
>   at
> org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.access$000(EmbeddedWebApplicationContext.java:84)
>   at
> org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:206)
>   at
> org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162)
>   at
> org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
>   ... 55 more Caused by: java.lang.ClassCastException:
> org.springframework.boot.context.web.ErrorPageFilter cannot be cast to
> org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
>   at
> com.futureprocessing.spring.ContainerConfiguration$1.customize(ContainerConfiguration.java:34)
>   at
> org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:67)
>   at
> org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:54)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
>   ... 71 more
> 
> 12-Dec-2015 14:54:39.321 SEVERE [http-apr-8080-exec-12]
> org.apache.catalina.startup.HostConfig.deployWAR Error deploying web
> application archive C:\Tomcat8\webapps\hello-gradle.war 
> java.lang.IllegalStateException: ContainerBase.addChild: start:
> org.apache.catalina.LifecycleException: Failed to start component
> [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/hello-gradle]]
>   at
> org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729)
>   at
> org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
>   at
> org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
>   at
> org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945)
>   at
> org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:466)
>   at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1583)
>   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:497)     at
> org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
>   at
> com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
>   at
> com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
>   at
> org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1460)
>   at
> org.apache.catalina.manager.HTMLManagerServlet.upload(HTMLManagerServlet.java:288)
>   at
> org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:209)
>   at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)     at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:729)  at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
>   at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>   at
> org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:136)
>   at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
>   at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>   at
> org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
>   at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
>   at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>   at
> org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
>   at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
>   at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>   at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
>   at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
>   at
> org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614)
>   at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
>   at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
>   at
> org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
>   at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
>   at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
>   at
> org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
>   at
> org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
>   at
> org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
>   at
> org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
>   at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
>   at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
>   at
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
>   at java.lang.Thread.run(Thread.java:745)
> 
> 12-Dec-2015 14:54:39.327 INFO [http-apr-8080-exec-12]
> org.apache.catalina.startup.HostConfig.deployWAR Deployment of web
> application archive C:\Tomcat8\webapps\hello-gradle.war has finished
> in 4,123 ms

It does say that the error occurred in my java file called ContainerConfiguration.java:

@Configuration
public class ContainerConfiguration {

    @Bean
    EmbeddedServletContainerCustomizer containerCustomizer(
            //all taken from the application.properties file
            @Value("${keystore.file}") String keystoreFile,
            @Value("${server.port}") final String serverPort,
            @Value("${keystore.pass}") final String keystorePass)
            throws Exception {

        // This is boiler plate code to setup https on embedded Tomcat
        // with Spring Boot:

        final String absoluteKeystoreFile = new File(keystoreFile)
                .getAbsolutePath();

        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
                tomcat.addConnectorCustomizers(connector -> {
                    connector.setPort(Integer.parseInt(serverPort));
                    connector.setSecure(true);
                    connector.setScheme("https");

                    Http11NioProtocol proto = (Http11NioProtocol) connector
                            .getProtocolHandler();
                    proto.setSSLEnabled(true);

                    proto.setKeystoreFile(absoluteKeystoreFile);
                    proto.setKeystorePass(keystorePass);
                    proto.setKeystoreType("JKS");
                    proto.setKeyAlias("abcd"); 
                });
            }
        };
    }
}

The error is on this line:

     tomcat.addConnectorCustomizers(connector -> {
                connector.setPort(Integer.parseInt(serverPort));
                connector.setSecure(true);
                connector.setScheme("https");

I don't get how errorPageFilter is casted to TomcatEmbeddedServletContainerFactory?

Does anyone know how I can fix this error?

EDIT:

I have also create a simple SpringBoot test app to demonstrate this error as well , you can find it here: https://github.com/Winghin2517/testSpringBootTomcat

There are only two classes in the github repo:

  1. ContainerConfiguration - where the offensive class is sitting.
  2. DemoApplication - the main method sits here

If you comment out the ContainerConfiguration and build a war file then upload it to a tomcat server, it works perfectly. If you uncomment out the ContainerConfiguration and build the war file and upload it, you will get the error.

回答1:

You're trying to customize SSL connections with your customizer. It's 100% legal if you are launching embedded tomcat server. So the flow is the following:

  • you customize tomcat setup
  • you launch the server

From the JavaDocs of EmbeddedServletContainerCustomizer

Strategy interface for customizing auto-configured embedded servlet containers. Any beans of this type will get a callback with the container factory before the container itself is started, so you can set the port, address, error pages etc.

Contrary, you cannot customize any of those things on already running tomcat.

Imagine what kind of security issues you could face if you had an option to change port / address of already running tomcat with just deploying a war file to it.

If you really need to customize the ssl connectors you need to go deep into xml configurations of tomcat.

A great and simple guide can be found here



回答2:

Why are your trying to configure embedded Tomcat when you want to deploy it on external container? I had similar problem and what finally worked for me was to completely exclude tomcat starter from the project dependencies. Since spring-boot-starter-tomcat is dependency of spring-boot-starter-web use something like this:

compile ('org.springframework.boot:spring-boot-starter-web') {
    exclude module: 'spring-boot-starter-tomcat'
}

And, of course, delete line that explicitly imports spring-boot-starter-tomcat (it's not needed, even in embedded mode).

You may have a problem if you want your app to be deployable in both ways - I didn't have to worry about this in my case.