easyExcel报错:java.lang.ClassNotFoundException解决
Contents
1概述
在前一篇文章中,easyExcel快速上手之后。在工作中的一个项目中,尝试照猫画虎的思路,准备使用easyExcel将页面上的数据,在浏览器上通过导出按钮,导出生成到本地的Excel文件中。
2 pom.xml文件中添加依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.10</version> </dependency>
3 启动报错
java.lang.ClassNotFoundException: org.apache.poi.util.DefaultTempFileCreationStrategy
4 错误解决思路
尝试解决的思路:
a 排除依赖
先是通过maven helper排查jar包依赖冲突,果然看到有类似下述冲突信息:
注意:这里的截图,是我时候复盘时的截图,冲突信息跟最初冲突并不完全一致。因为,中间不停的尝试解决错误,更换了不同的插件版本,有可能导致把之前的冲突给解决了,或者带来了新的jar包冲突。
然后:
在pom.xml文件中,通过exclusion标签排除依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.10</version> <exclusions> <exclusion> <artifactId>poi-ooxml-schemas</artifactId> <groupId>org.apache.poi</groupId> </exclusion> </exclusions> <exclusions> <exclusion> <artifactId>poi-ooxml</artifactId> <groupId>org.apache.poi</groupId> </exclusion> </exclusions> </dependency>
依然报错!
b 引入依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.10</version> <!-- <exclusions>--> <!-- <exclusion>--> <!-- <artifactId>poi-ooxml-schemas</artifactId>--> <!-- <groupId>org.apache.poi</groupId>--> <!-- </exclusion>--> <!-- </exclusions>--> <!-- <exclusions>--> <!-- <exclusion>--> <!-- <artifactId>poi-ooxml</artifactId>--> <!-- <groupId>org.apache.poi</groupId>--> <!-- </exclusion>--> <!-- </exclusions>--> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
果然,错误解决了。注意,这里的版本号是3.17时,才不会报错。如果引入的poi和poi-ooxml版本为4.10同样会报错。
c 错误复盘
经过比对,工作中的这个项目和上一个自己练手的easyexcel快速上手项目中,对于easyexcel版本有区别。练手项目中,引入的2.2.9版本,而工作中,这个项目使用的是2.2.10版本。
然后,尝试将当前工程中对于poi和poi-ooxml这两个插件的引入去掉,同时将easyexcel版本降低到2.2.9,然后,也并没有报错。
2021.6.1补充,上面的讲版本降低到2.2.9,并且去掉对于poi和poi-ooxml这两个插件的依赖,这种情况还是会报错!
2021-06-01 19:08:15.413 [http-nio-7083-exec-1] ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost]. [/samweb].[dispatcherServlet] - - - - Servlet.service() for servlet [dispatcherServlet] in context with path [/samweb] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/util/DefaultTempFileCreationStrategy] with root cause java.lang.ClassNotFoundException: org.apache.poi.util.DefaultTempFileCreationStrategy at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
正确的排错,还是得使用上述步骤b的引用的配置,是正确的!!!
5 小结及参考链接
对于新手而言,遇到问题,就是一定要想法不停的动手多尝试,争取找到解决错误的办法。同时,也应该及时的向同事、朋友求助。看到,有网友说,曾经被遇到的报错折腾了3天~~
参考链接1:https://blog.csdn.net/dadachenchen/article/details/108758972
参考链接2:https://www.codenong.com/cs105266552/
参考链接3:https://blog.csdn.net/KeepLearnZhangXiaoBo/article/details/106915805
6 补充错误 org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause
28-May-2021 13:51:17.812 SEVERE [http-nio-8080-exec-7] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause
org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
在postman测试一个springMVC的工程时,在postman中指定了Content-Type=
将postman参数不要手工去设置,留空,让浏览器自动识别该文件即可!
The problem is that you are setting the Content-Type by yourself, let it be blank. Google Chrome will do it for you. The multipart Content-Type needs to know the file boundary, and when you remove the Content-Type, Postman will do it automagically for you.