springboot打war包至tomcat报Cannot cast org.springframework.web.SpringServletContainerlnitializer to j...

本文介绍了一个SpringBoot项目在部署到Tomcat过程中遇到的ClassCastException错误,并详细记录了解决步骤,包括排除内置Tomcat及调整servlet-api依赖。

项目简介

最近,公司新起了个项目,感觉使用springboot替代当前流行已久的ssm(spring+springmvc+mybatis)开发框架,将要成为趋势,因此我们项目组决定尝试用springboot构建这个新项目。

我们使用idea很快搭建出了以springboot 2.0.3为版本的父子项目,pom中引入相关依赖jar包,用了两天时间创建项目所需工具类,生成一些初步实体类,配置文件写入必要的配置信息,连接数据库,启动端口号,加入mybatis-generator自动生成工具等,写入一些简单的接口测试后,本地调试,项目启动顺利。

使用springboot,第一感觉是配置文件比使用ssm少了很多,没有xml之类的配置,配置也很简单,许多东西springboot都给集成好了,只需用就行,因此创建一个web项目几乎是可用刹那来形容,毫不费力。

项目测试都运行正常,我们就将其部署至测试环境上。我们的项目是以生成war的形式发布测试环境的tomcat里。起初运维人员使用tomcat7版本,出现版本不符合,又更换成tomcat8。启动出现了错误。

错误内容

springboot war发布至tomcat错误
03-Jul-2018 13:39:01.845 SEVERE [localhost-startStop-1] org.apache.catalina.startup.ContextConfig.processServle
tContainerlnitializers Failed to process JAR found at URL [/back] for ServletContainerlnitializers for context
with name [{1}]
java.io.IOException: java.lang.ClassCastException: Cannot cast org.springframework.web.SpringServletContainerl
nitializer to javax.servlet.ServletContainerlnitializer
at org.apache.catalina.startup.WebappServiceLoader.loadServices(WebappServiceLoader.java:185)
at org.apache.catalina.startup.WebappServiceLoader.load(WebappServiceLoader.java:151)
at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:159
7)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1125)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:768)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:303)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startlnternaKStandardContext.java:5058)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildlnternaKContainerBase.java:726)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:697)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:976)
at org.apache.catalina.startup.HostConfigDeployWar.run(HostConfig.java:1762)atjava.util.concurrent.ExecutorsDeployWar.run(HostConfig.java:1762)atjava.util.concurrent.ExecutorsRunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: Cannot cast org.springframework.web.SpringServletContainerlnitializer
to javax.servlet.ServletContainerlnitializer
at java.lang.Class.cast(Class.java:3361)
at org.apache.catalina.startup.WebappServiceLoader.loadServices(WebappServiceLoader.java:182)
…19 more

java.io.IOException: java.lang.ClassCastException: Cannot cast org.springframework.web.SpringServletContainerlnitializer to javax.servlet.ServletContainerlnitializer

这是什么原因造成的呢?经过多方面查找,找到如下两个原因

  • springboot默认提供内嵌的tomcat包,因此打包时需要将自带的tomcat包排除,否则会与外部tomcat冲突,解决方式spring-boot-starter-web下加入exclusions排除spring-boot-starter-tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

有说使用如下方式,但是我们当时使用,未起作用,不知道是不是为清理干净的原因

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
  • 若引入了javax.servlet-api,还需将其排除掉,否则也会与tomcat中的servlet-api包冲突。处理方式加入provided
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

经过以上两步,重新打包发布,启动成功。

这是初次正式使用springboot框架搭建项目,出现问题在所难免,后面应该还会出现各种不可预知的问题,但是遇见错误bug不可怕,可怕的是没有任何错误信息,不知道错误藏身之地。发现错误,解决错误,是进步,也是一种快乐与收获。发现错误,解决bug,是项目成功的一大步。

(base) PS D:\cas-env> docker build --no-cache -t cas-final:v1 . [+] Building 17.9s (16/16) FINISHED docker:desktop-linux => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 747B 0.0s => [internal] load metadata for docker.io/apereo/cas:6.6.0 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => CACHED [ 1/11] FROM docker.io/apereo/cas:6.6.0@sha256:dd23e47a15a93256dece4e9ea320080a67298fd9fc0a6f3a63d121c7a5467e03 0.0s => => resolve docker.io/apereo/cas:6.6.0@sha256:dd23e47a15a93256dece4e9ea320080a67298fd9fc0a6f3a63d121c7a5467e03 0.0s => [internal] load build context 6.2s => => transferring context: 146.95MB 6.2s => [ 2/11] RUN rm -rf /usr/local/tomcat/webapps/cas 0.5s => [ 3/11] RUN rm -f /usr/local/tomcat/webapps/cas.war 0.7s => [ 4/11] RUN rm -rf /etc/cas/config/* 0.9s => [ 5/11] COPY target/cas.war /tmp/cas.war 0.5s => [ 6/11] RUN mkdir -p /usr/local/tomcat/webapps/cas && cd /usr/local/tomcat/webapps/cas && jar -xf /tmp/cas.war && rm /tmp/cas.war 2.2s => [ 7/11] RUN mkdir -p /etc/cas/config 0.5s => [ 8/11] COPY src/main/resources/application.yml /etc/cas/config/application.yml 0.0s => [ 9/11] COPY src/main/resources/thekeystore /etc/cas/thekeystore 0.0s => [10/11] COPY src/main/resources/services /etc/cas/services 0.0s => [11/11] RUN chmod -R 777 /etc/cas/services 0.4s => exporting to image 7.6s => => exporting layers 5.8s => => exporting manifest sha256:6704e1cde83466643005a96aad5280677321f6485c6076938d5538dd53070483 0.0s => => exporting config sha256:2aacc2a71ebc8fa7f67569d3ce8d4520b878fbed40ce4e4574415c67c33fc63f 0.0s => => exporting attestation manifest sha256:1898b1619939f7d85fe6c487fe97975aa67c454c7bac203f314ccea5083bae98 0.0s => => exporting manifest list sha256:cce54c96c5f3d4530aa16aac74e13462bcd23f5a9d9b3cf3d556bd2036bf7627 0.0s => => naming to docker.io/library/cas-final:v1 0.0s => => unpacking to docker.io/library/cas-final:v1 1.6s View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/ym1vcttunqzb94vyo9frvw5bo (base) PS D:\cas-env> docker rm -f cas cas (base) PS D:\cas-env> docker run -d --name cas -p 9443:8443 --add-host host.docker.internal:host-gateway cas-final:v1 f52ec2a53d6f62eb09cc40aa8f3e4ae29fa3fc531bfe122ab224b481053d8b6a (base) PS D:\cas-env> docker logs -f cas -e Running CAS @ cas.war /docker/entrypoint.sh: 10: [: false: unexpected operator /docker/entrypoint.sh: 14: [: false: unexpected operator 2025-12-26 05:08:50,949 INFO [org.apereo.cas.configuration.CasConfigurationPropertiesValidator] - <Validated CAS property sources and configuration successfully.> 2025-12-26 05:08:52,489 DEBUG [org.springframework.boot.devtools.restart.Restarter] - <Creating new Restarter for thread Thread[main,5,main]> _ ____ _____ ____ _____ ___ ____ _ ____ / \ | _ \| ____| _ \| ____/ _ \ / ___| / \ / ___| / _ \ | |_) | _| | |_) | _|| | | | | | / _ \ \___ \ / ___ \| __/| |___| _ <| |__| |_| | | |___ / ___ \ ___) | /_/ \_\_| |_____|_| \_\_____\___/ \____/_/ \_\____/ CAS Version: 6.6.0 CAS Branch: 6.6.x CAS Commit Id: cff699f5de01c2a176c311b394447a4513107864 CAS Build Date/Time: 2022-09-04T02:24:14Z Spring Boot Version: 2.7.3 Spring Version: 5.3.22 Java Home: /opt/java/openjdk Java Vendor: Eclipse Adoptium Java Version: 11.0.16.1 JVM Free Memory: 582 MB JVM Maximum Memory: 4 GB JVM Total Memory: 1 GB OS Architecture: amd64 OS Name: Linux OS Version: 5.15.167.4-microsoft-standard-WSL2 OS Date/Time: 2025-12-26T05:08:52.678167 OS Temp Directory: /tmp ------------------------------------------------------------ Apache Tomcat Version: Apache Tomcat/9.0.65 ------------------------------------------------------------ 2025-12-26 05:08:52,734 INFO [org.apereo.cas.configuration.DefaultCasConfigurationPropertiesSourceLocator] - <Configuration files found at [/etc/cas/config] are [[file [/etc/cas/config/application.yml]]] under profile(s) [[standalone]]> 2025-12-26 05:08:52,754 INFO [org.apereo.cas.configuration.loader.YamlConfigurationPropertiesLoader] - <Found settings [[cas.authn.accept.enabled, cas.authn.jdbc.query[0].dialect, cas.authn.jdbc.query[0].driverClass, cas.authn.jdbc.query[0].fieldPassword, cas.authn.jdbc.query[0].password, cas.authn.jdbc.query[0].passwordEncoder.type, cas.authn.jdbc.query[0].sql, cas.authn.jdbc.query[0].url, cas.authn.jdbc.query[0].user, cas.logout.follow-service-redirects, cas.service-registry.core, cas.service-registry.json.location, logging.level.com.zaxxer.hikari, logging.level.org.apereo.cas, logging.level.org.apereo.cas.adaptors.jdbc, server.port, server.servlet.context-path, server.ssl.enabled, server.ssl.key-password, server.ssl.key-store, server.ssl.key-store-password, spring.cloud.config.enabled, spring.main.allow-bean-definition-overriding]] in YAML file [file [/etc/cas/config/application.yml]]> 2025-12-26 05:08:52,890 ERROR [org.apereo.cas.configuration.CasConfigurationPropertiesValidator] - <class org.springframework.core.convert.ConverterNotFoundException cannot be cast to class org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException (org.springframework.core.convert.ConverterNotFoundException and org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException are in unnamed module of loader org.springframework.boot.loader.LaunchedURLClassLoader @1a86f2f1)> 2025-12-26 05:08:52,890 INFO [org.apereo.cas.configuration.CasConfigurationPropertiesValidator] - <Validated CAS property sources and configuration successfully.> 2025-12-26 05:08:52,891 INFO [org.apereo.cas.web.CasWebApplication] - <The following 1 profile is active: "standalone"> 2025-12-26 05:08:55,868 DEBUG [org.apereo.cas.tomcat.CasTomcatServletWebServerFactory] - <Code archive: /docker/cas/war/cas.war> 2025-12-26 05:08:55,868 DEBUG [org.apereo.cas.tomcat.CasTomcatServletWebServerFactory] - <Document root: /docker/cas/war/cas.war> 2025-12-26 05:08:58,791 INFO [org.apereo.cas.config.CasCoreServicesConfiguration] - <Runtime memory is used as the persistence storage for retrieving and persisting service definitions. Changes that are made to service definitions during runtime WILL be LOST when the CAS server is restarted. Ideally for production, you should choose a storage option (JSON, JDBC, MongoDb, etc) to track service definitions.> 2025-12-26 05:09:03,663 WARN [org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration] - < Using generated security password: 32531d26-bc1c-41a9-b9ce-294ccabb3162 This generated password is for development use only. Your security configuration must be updated before running your application in production. > 2025-12-26 05:09:03,779 DEBUG [org.apereo.cas.web.security.CasWebSecurityConfigurerAdapter] - <Configuring protocol endpoints [[/login/**, /logout/**, /validate/**, /serviceValidate/**, /p3/serviceValidate/**, /proxyValidate/**, /p3/proxyValidate/**, /proxy/**, /webjars/**, /js/**, /css/**, /images/**, /static/**, /error, /favicon.ico]] to exclude/ignore from web security> 2025-12-26 05:09:04,408 INFO [org.springframework.security.web.access.channel.ChannelProcessingFilter] - <Validated configuration attributes> 2025-12-26 05:09:04,422 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will secure any request with [org.springframework.security.web.access.channel.ChannelProcessingFilter@5cf2f5d6, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@428eb3d5, org.springframework.web.filter.CorsFilter@5ddd84d2, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1640f20f, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@3fd5d679, org.springframework.security.web.access.ExceptionTranslationFilter@12448de1, org.springframework.security.web.access.intercept.AuthorizationFilter@636fccb0]> 2025-12-26 05:09:04,466 DEBUG [org.apereo.cas.web.security.CasWebSecurityConfigurerAdapter] - <Configuring protocol endpoints [[/login/**, /logout/**, /validate/**, /serviceValidate/**, /p3/serviceValidate/**, /proxyValidate/**, /p3/proxyValidate/**, /proxy/**, /webjars/**, /js/**, /css/**, /images/**, /static/**, /error, /favicon.ico]] to exclude/ignore from web security> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/login/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/logout/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/validate/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/serviceValidate/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/p3/serviceValidate/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/proxyValidate/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/p3/proxyValidate/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/proxy/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/webjars/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/js/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/css/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/images/**']> 2025-12-26 05:09:04,466 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/static/**']> 2025-12-26 05:09:04,467 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/error']> 2025-12-26 05:09:04,467 INFO [org.springframework.security.web.DefaultSecurityFilterChain] - <Will not secure Ant [pattern='/favicon.ico']> 2025-12-26 05:09:05,738 WARN [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext] - <Exception encountered during context initialization - cancelling refresh attempt: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'cas-org.apereo.cas.configuration.CasConfigurationProperties': Could not bind properties to 'CasConfigurationProperties' : prefix=cas, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'cas.service-registry.core' to org.apereo.cas.configuration.model.core.services.ServiceRegistryCoreProperties>
最新发布
12-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值