EasyExcel 报错 NoClassDefFoundError org/ehcache/config/builders/CacheManagerBuilder
特此郑重声明!该文章是原创作品,小编编写实属不易 ,帮忙点赞关注一下~转载小伙伴请注明出处!
EasyExcel 导入几十万数据报错
今天在执行海量数据数据导入的过程中,当Excel 文件内容超过30W ,就出现导入失败。
报错内容如下:
com.alibaba.excel.exception.ExcelAnalysisException: java.lang.NoClassDefFoundError: org/ehcache/config/builders/CacheManagerBuilder
at com.alibaba.excel.analysis.ExcelAnalyserImpl.<init>(ExcelAnalyserImpl.java:57) ~[easyexcel-2.2.10.jar:na]
at com.alibaba.excel.ExcelReader.<init>(ExcelReader.java:145) ~[easyexcel-2.2.10.jar:na]
at com.alibaba.excel.read.builder.ExcelReaderBuilder.build(ExcelReaderBuilder.java:193) ~[easyexcel-2.2.10.jar:na]
at com.alibaba.excel.read.builder.ExcelReaderBuilder.sheet(ExcelReaderBuilder.java:229) ~[easyexcel-2.2.10.jar:na]
at com.alibaba.excel.read.builder.ExcelReaderBuilder.sheet(ExcelReaderBuilder.java:217) ~[easyexcel-2.2.10.jar:na]
at com.zkh360.customer.manage.controller.demo.ImportFileServive.importFile(ImportFileServive.java:47) ~[classes/:na]
at com.zkh360.customer.manage.controller.demo.FileController.importFile(FileController.java:28) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:665) [javax.servlet-api-4.0.1.jar:4.0.1]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) [javax.servlet-api-4.0.1.jar:4.0.1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) [tomcat-embed-websocket-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123) [druid-1.1.10.jar:1.1.10]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320) [spring-security-web-5.3.4.RELEASE.jar:5.3.4.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126) [spring-security-web-5.3.4.RELEASE.jar:5.3.4.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90) [spring-security-web-5.3.4.RELEASE.jar:5.3.4.RELEASE]
本人EasyExcel 使用的版本:Version 2.2.10
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
</dependency>
代码如下:
@Slf4j
@Service
public class ImportFileServive implements FileImport {
@Override
public void importFile(MultipartFile file) {
try (InputStream in = file.getInputStream()) {
Workbook workBook = null;
List<ExcelDataVo> list = EasyExcel.read(file.getInputStream()).head(ExcelDataVo.class).sheet().headRowNumber(1).doReadSync();
log.info("实际获取数据:{}",list.size())
} catch (IOException e) {
log.error("importExcel failed IOException", e);
}
}
}
@Data
public class ExcelDataVo {
@ExcelProperty("Id")
private String id;
@ExcelProperty("custNumber")
private String custName;
@ExcelProperty("custNumber")
private String custNumber;
@ExcelProperty("text01")
private String text01;
@ExcelProperty("text02")
private String text02;
@ExcelProperty("text03")
private String text03;
@ExcelProperty("text04")
private String text04;
@ExcelProperty("text05")
private String text05;
}
问题定位
经过定位EasyExcel官网 发现
EasyExcel 在使用过程中 超过5M 会使用Ehcache
我这边本地代码 由于Ehcache 是一个老版本3.0 的版本,初步猜测 版本导致 。
解决思路
方案一
直接 升级ehcache 版本<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.9.9</version>
<scope>compile</scope>
</dependency>
PS : 注意easyexcel 如果使用 低版本的 easyexcel 需要将其 exclusions 掉
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
<exclusions>
<exclusion>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>
方案二 采用MapCache 替换解决
有的小伙伴在开发过程,可能改的老代码强制升级可能让原始代码使用Ehcache 功能过程收到影响。
对于一个优秀程序员来说,老代码程序能跑就不要轻易大改!!!
在原始代码中加入 readCache(new MapCache())
@Slf4j
@Service
public class ImportFileServive implements FileImport {
@Override
public void importFile(MultipartFile file) {
try (InputStream in = file.getInputStream()) {
Workbook workBook = null;
List<ExcelDataVo> list = EasyExcel.read(file.getInputStream()).readCache(new MapCache()).head(ExcelDataVo.class).sheet().headRowNumber(1).doReadSync();
log.info("实际获取数据:{}",list.size())
} catch (IOException e) {
log.error("importExcel failed IOException", e);
}
}
}
MapCache 本质就是一个HashMap
运行程序 !解决!