EasyExcel 报错 NoClassDefFoundError org/ehcache/config/builders/CacheManagerBuilder
Hereby solemnly declare! This article is an original work, and it is not easy for the editor to write it. Help me like it and follow it~ Reprint friends, please indicate the source!
EasyExcel reports an error when importing hundreds of thousands of data
Today, in the process of importing massive data, when the content of the Excel file exceeds 30W, the import fails.
The error message is as follows:
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]
The version I use EasyExcel: Version 2.2.10
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
</dependency>
code show as below:
@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;
}
identify the problem
After locating the EasyExcel official website , I found that
EasyExcel will use Ehcache when it exceeds 5M during use.
My local code is because Ehcache is an old version 3.0, which is caused by a preliminary guess.
Solutions
Option One
Upgrade the ehcache version directly<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.9.9</version>
<scope>compile</scope>
</dependency>
PS: Pay attention to easyexcel If you use a lower version of easyexcel, you need to drop its 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>
Solution 2 uses MapCache to replace the solution
Some small partners are in the development process, and the old code that may be modified may be forced to upgrade, which may affect the process of using the Ehcache function of the original code .
For an excellent programmer, if the old code program can run, don't change it easily! ! !
Add readCache(new MapCache()) to the original code
@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 is essentially a HashMap
Run the program! solve!