1.最近的项目中使用到了Drools作为规则引擎,今天在开发决策表的导入功能时(xls文件转drl),网上提供的简要例子始终通不过,报找不到类的错误:
示例如下:
try { ruleTableConvertDRL("D:\\calculation.xls"); } catch (IOException e) { e.printStackTrace(); }
/** * 将xls决策表解析为普通规则文件 * @throws IOException */ public File ruleTableConvertDRL(String ruleTablePath) throws IOException { SpreadsheetCompiler compiler = new SpreadsheetCompiler(); File inFile = new File(ruleTablePath); File outFile = new File("D:\\outFile.txt"); InputStream xlsStream = null; OutputStream fos = null; OutputStreamWriter osw = null; BufferedWriter out = null; try { xlsStream = new FileInputStream(inFile); fos = new FileOutputStream(outFile); osw = new OutputStreamWriter(fos); out = new BufferedWriter(osw); String strRule = compiler.compile(xlsStream, InputType.XLS); out.write(strRule); out.flush(); System.out.println("\n" + strRule); } catch (IOException e) { throw new IOException("没找到文件或写入文件错误"); }finally{ xlsStream.close(); fos.close(); osw.close(); out.close(); } return outFile; }
2.报错如下:
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.Workbook.close()V at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:982) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
Caused by: java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.Workbook.close()V at org.drools.decisiontable.parser.xls.ExcelParser.parseWorkbook(ExcelParser.java:124) at org.drools.decisiontable.parser.xls.ExcelParser.parseFile(ExcelParser.java:83) at org.drools.decisiontable.SpreadsheetCompiler.compile(SpreadsheetCompiler.java:103) at org.drools.decisiontable.SpreadsheetCompiler.compile(SpreadsheetCompiler.java:76)
3.首先我的第一反应就是应该是jar包冲突,我当时用的drools版本是6.4,poi是3.9:
4.后来经过多次尝试,选择drools为7.0版本,poi为3.15版本时,测试通过,后台可以正常输出决策表内容
5.drools和poi版本如下:
<dependency> <groupId>org.kie</groupId> <artifactId>kie-api</artifactId> <version>7.0.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>7.0.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-compiler</artifactId> <version>7.0.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-decisiontables</artifactId> <version>7.0.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-templates</artifactId> <version>7.0.0.Final</version> </dependency>
<!-- apahce poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.15</version> </dependency>