Spring Boot项目jdk9运行报错,怎么处理都不行

jdk9与jdk8相比有很大的区别

  • JDK8:JDK8的部署是一个单体模式,即使项目内容少,也需要一整套的JRE环境。
  • JDK9:JDK9引入模块后,能将所有的类组织成模块形式。在新建项目时,不再需要整套的JRE环境。

并且jdk9在发布的时候有一些JDK8中的jar依赖没有集成进去,因此在使用一些版本的Spring Boot项目的时候就会报一些jar包缺失的错误。

org.springframework.context.ApplicationContextException: 
Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource 
[org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: 
Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration': 
Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 
'spring.http.encoding-org.springframework.boot.autoconfigure.web.HttpEncodingProperties': Initialization of bean
 failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/ValidationException; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.http.encoding-
 org.springframework.boot.autoconfigure.web.HttpEncodingProperties': Initialization of bean failed; nested exception is 
 java.lang.NoClassDefFoundError: javax/xml/bind/ValidationException

根本的原因就是jdk9的jar包的缺失。

解决方法

在项目的pom.xml文件中引入缺失的jar包:


<dependency>
    <groupId>javax.xml.bindgroupId>
    <artifactId>jaxb-apiartifactId>
    <version>2.3.0version>
dependency>
<dependency>
    <groupId>com.sun.xml.bindgroupId>
    <artifactId>jaxb-implartifactId>
    <version>2.3.0version>
dependency>
<dependency>
    <groupId>com.sun.xml.bindgroupId>
    <artifactId>jaxb-coreartifactId>
    <version>2.3.0version>
dependency>
<dependency>
    <groupId>javax.activationgroupId>
    <artifactId>activationartifactId>
    <version>1.1.1version>
dependency>

然后重新启动项目!!

2022-02-22 12:44:29.375  INFO 12020 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2022-02-22 12:44:29.375  INFO 12020 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2022-02-22 12:44:29.423  INFO 12020 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 27ms. Found 0 Redis repository interfaces.
2022-02-22 12:44:29.995  INFO 12020 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8082 (http)
2022-02-22 12:44:30.012  INFO 12020 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-02-22 12:44:30.012  INFO 12020 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.35]

启动成功!!

你可能感兴趣的:(spring,boot,java,spring)