Spring Boot で java.lang.NoClassDefFoundError: org/dom4j/io/STAXEventReader
最近、「Spring Boot プログラミング入門」の本で Spring Boot の勉強を始めました。
モデルのサンプルのところでエラーが発生したため、その対処方法をメモっておきます。
環境
Spring Boot 1.5.4
エラーの内容
2017-06-23 12:00:20.150 ERROR 9138 --- [ main] o.s.boot.SpringApplication : Application startup failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/dom4j/io/STAXEventReader at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] .....
ということで、java.lang.NoClassDefFoundError: org/dom4j/io/STAXEventReader
というエラーのようです。
対処方法
ググってみると、以下のページに対処方法がありました:
どうやら、Hibernate core が依存している dom4j-1.6.1 がこのエラーを吐いているので、dom4j-1.6 に入れ替えれば良いようです。
「Spring Boot プログラミング入門」のサンプルでは、spring-boot-starter-data-jpa の依存関係に dom4j が含まれているため、pom.xml の該当部分を以下のように変更します。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <exclusions> <!-- Exclude dom4j to avoid version conflicts (we have 1.6, this drags in 1.6.1) --> <exclusion> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> </exclusion> </exclusions> </dependency> <!-- add dom4j in the correct version --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6</version> </dependency>
これでエラーが出なくなりました。