java10运行springboot项目出现:Type javax.xml.bind.JAXBContext not present

原因:java9模块化的概念使得JAXB默认没有加载;

jaxb-api是存在jdk中的,只是默认没有加载而已,手动引入即可。

推荐方式:(作者采用的此方式)

<!-- jaxb模块引用 - start -->
<dependency>
   <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>
<!-- jaxb模块引用 - end -->

java9模块命令方式:

--add-modles java.xml.bind

java9默认未加载模块:

javax.activation
javax.corba
javax.transaction
javax.xml.bind
javax.xml.ws
javax.xml.ws.annotation

推荐大家升级到JDK11,此为LTS版本(Oracle长期支持版本),BUG更少、兼容性更强


后记

升级到SpringBoot2.3后,spring-security-oauth2引入上述介绍的4个依赖将会报错如下错误:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler]: Factory method 'oAuth2AccessDeniedHandler' threw exception; nested exception is java.lang.NoClassDefFoundError: com/sun/xml/bind/Util
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	... 33 common frames omitted
Caused by: java.lang.NoClassDefFoundError: com/sun/xml/bind/Util
	at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:113) ~[jaxb-impl-2.3.0.jar:2.3.3]
	at com.sun.xml.bind.v2.JAXBContextFactory.createContext(JAXBContextFactory.java:35) ~[jaxb-runtime-2.3.3.jar:2.3.3]
	at javax.xml.bind.ContextFinder.find(ContextFinder.java:404) ~[jaxb-api-2.3.1.jar:2.3.0]
	at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721) ~[jaxb-api-2.3.1.jar:2.3.0]
	at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662) ~[jaxb-api-2.3.1.jar:2.3.0]

正确依赖如下:

<!-- jaxb模块引用 - start -->
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
</dependency>
<!-- jaxb模块引用 - end -->
评论 13 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:书香水墨 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值