目录
1 问题描述
java.lang.ClassCastException: java.util.HashMap$KeySet cannot be cast to java.util.List
2 问题分析
2.1 Java
1、Set不能直接强转为List,强转会报错。
Map<String, Integer> map= new HashMap<>();
List<String> list= (List<String>) map.keySet();
解决办法:使用new ArrayList()将Set转换为List。
Map<String, Integer> map= new HashMap<>();
List<String> list= new ArrayList(map.keySet());
更多类型之间相互转换请参考以下博客。
2.2 EasyPoi
1、使用EasyPoi进行内容替换时,WordExportUtil.exportWord07()第一个传参类型为org.apache.poi.xwpf.usermodel.XWPFDocument,导致转换成cn.afterturn.easypoi.word.entity.MyXWPFDocument报错。
public void replaceContent(HttpServletResponse response) {
try {
//读文件
ClassPathResource cpr = new ClassPathResource("/doc/模板.docx");
XWPFDocument document = new XWPFDocument(cpr.getInputStream());
//生成文本内容Map
Map<String, Object> contentMap = new HashMap<>();
contentMap.put("xudongmaster1", "旭东怪1");
contentMap.put("xudongmaster2", "旭东怪2");
//替换文本内容
WordExportUtil.exportWord07(document, contentMap);
//返回流
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + new String("模板.docx".getBytes("utf-8"), "ISO-8859-1"));
OutputStream outputStream = response.getOutputStream();
document.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
解决办法:
WordExportUtil.exportWord07()第一个传参类型改为cn.afterturn.easypoi.word.entity.MyXWPFDocument。
public void replaceContent(HttpServletResponse response) {
try {
//读文件
ClassPathResource cpr = new ClassPathResource("/doc/模板.docx");
XWPFDocument document = new MyXWPFDocument(cpr.getInputStream());
//生成文本内容Map
Map<String, Object> contentMap = new HashMap<>();
contentMap.put("xudongmaster1", "旭东怪1");
contentMap.put("xudongmaster2", "旭东怪2");
//替换文本内容
WordExportUtil.exportWord07(document, contentMap);
//返回流
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + new String("模板.docx".getBytes("utf-8"), "ISO-8859-1"));
OutputStream outputStream = response.getOutputStream();
document.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2.3 JFreeChart
1、使用ChartFactory.createScatterPlot()创建散点图方法获取JFreeChart类型的对象时,不能使用JFreeChart.getCategoryPlot()方法来获取org.jfree.chart.plot.CategoryPlot类型的plot对象,因为plot对象本身是org.jfree.chart.plot.XYPlot类型,而JFreeChart.getCategoryPlot()方法会直接将org.jfree.chart.plot.CategoryPlot类型强转为org.jfree.chart.plot.XYPlot类型,这会直接导致报错。
解决办法:使用JFreeChart.getXYPlot()方法。
2.4 EasyExcel
1、自定义处理器使用了org.apache.poi.xssf.usermodel.XSSFSheet对象,但是传了org.apache.poi.xssf.streaming.SXSSFSheet对象,导致报错。
解决办法:注册处理器之前先调用.inMemory(Boolean.TRUE)方法,这样就会传org.apache.poi.xssf.usermodel.XSSFSheet对象。
ExcelWriter excelWriter = EasyExcel.write(fileOutputStream)
.inMemory(Boolean.TRUE).registerWriteHandler(new CustomWaterMarkHandler(waterMarkList)).build();
2.5 Mybatis-Plus
1、分页插件使用了shardingsphere,但是shardingsphere依赖版本太低了,导致报错。
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC2</version>
</dependency>
解决办法:
将shardingsphere依赖版本升级至4.1.1即可。
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>