NoClassDefFoundError: javax/xml/bind/UnmarshalException - Spring Security oauth2
Posted
技术标签:
【中文标题】NoClassDefFoundError: javax/xml/bind/UnmarshalException - Spring Security oauth2【英文标题】: 【发布时间】:2018-05-01 08:50:16 【问题描述】:目前我正在致力于保护 REST SpringBoot 服务。我决定将 oauth2 与 JWT 一起使用。但是我在应用程序启动时遇到了那个奇怪的错误。我相信 spring-security-core 和 spring-security-jwt 版本肯定有一些错误。这是堆栈:
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-11-17 11:47:34.428 ERROR 9604 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/UnmarshalException
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at
org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527) [spring-boot-maven-plugin-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at java.base/java.lang.Thread.run(Thread.java:844) [na:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/UnmarshalException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 26 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/UnmarshalException
at org.springframework.security.oauth2.provider.error.DefaultOAuth2ExceptionRenderer.geDefaultMessageConverters(DefaultOAuth2ExceptionRenderer.java:117) ~[spring-security-oauth2-2.2.0.RELEASE.jar:na]
...
at
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 27 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.UnmarshalException
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:563) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496) ~[na:na]
... 51 common frames omitted
这是我的 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!-- Dependency for embed mongo testing -->
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
**<!-- Dependency for web security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>**
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
我在该配置中有什么遗漏的吗?
【问题讨论】:
【参考方案1】:你必须包含 Jaxb Api,所以在你的 pom.xml
中你应该添加它的依赖:
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
【讨论】:
非常感谢亚历山德罗。但现在我得到了原因:java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory 那在我意识到将我的 jdk 从 jdk-9.0.1 更改为 jdk1.8.0_151,它可以工作!但我不明白为什么 jdk-9 不包含 jaxb 支持?!
【讨论】:
哦,Java 9 实现了 JPMS(Java 平台模块系统)所以你必须添加 jaxb 模块见here以上是关于NoClassDefFoundError: javax/xml/bind/UnmarshalException - Spring Security oauth2的主要内容,如果未能解决你的问题,请参考以下文章
java.lang.NoClassDefFoundError: org/jboss/logging/Logger
java.lang.NoClassDefFoundError: com/opensymphony/xwork2/util/logging/LoggerFacto
Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.ClassVisitor