如何修复 java.lang.ClassCastException:.Review 在加载器的未命名模块中 'app'
How to fix java.lang.ClassCastException:.Review are in unnamed module of loader 'app'
我对特定的 class Helpers.java 进行了一些更改,每当我尝试对特定端点进行更改时,我都会收到此错误消息。堆栈跟踪指向助手的 class.
java.lang.ClassCastException: class com.clever.bas.model.Transaction cannot be cast to class com.clever.bas.model.Review (com.clever.bas.model.Transaction and com.clever.bas.model.Review are in unnamed module of loader 'app')
这是助手 class 的样子
public static InputStream writeToExcel(CompletableFuture<ArrayList<Review>> data) throws Exception {
//Blank workbook
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("transaction report");
AtomicInteger rowNum = new AtomicInteger(1);
AtomicBoolean isHeaderSet = new AtomicBoolean(false);
data.get().forEach(review -> {
if (!isHeaderSet.get()){
createHeader(review, sheet);
isHeaderSet.set(true);
}
XSSFRow row = sheet.createRow(rowNum.getAndIncrement());
ObjectMapper mapObject = new ObjectMapper();
Map<String, Object> mapObj = mapObject.convertValue(review, Map.class);
AtomicInteger cellNum = new AtomicInteger();
mapObj.forEach((key, value) -> {
XSSFCell cell = row.createCell(cellNum.getAndIncrement());
cell.setCellValue(key);
if (value instanceof Integer)
cell.setCellValue((Integer) value);
else if (value instanceof BigDecimal)
cell.setCellValue(((BigDecimal) value).doubleValue());
else if (value instanceof Long)
cell.setCellValue((Long) value);
else if (value instanceof Boolean)
cell.setCellValue((Boolean) value);
else cell.setCellValue((String) value);
});
});
try {
//Write the workbook in file system
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
byte[] barray = bos.toByteArray();
InputStream is = new ByteArrayInputStream(barray);
// InputStream is = new FileInputStream("transaction.xlsx");
// workbook.write(is);
System.out.println("transaction.xlsx written successfully on disk.");
return is;
// out.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
private static void createHeader(Review review, XSSFSheet sheet) {
XSSFRow row = sheet.createRow(0);
ObjectMapper mapObject = new ObjectMapper();
Map<String, Object> mapObj = mapObject.convertValue(review, Map.class);
AtomicInteger cellNum = new AtomicInteger();
mapObj.forEach((key, value) -> {
XSSFCell cell = row.createCell(cellNum.getAndIncrement());
cell.setCellValue(key);
});
}
我想不出我返回的是交易类型而不是评论。这是我在我的服务中调用助手 class 的地方。
public InputStream downloadReviewReport(UriInfo uriInfo) throws Exception {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String userID = queryParams.getFirst("userID");
CompletableFuture<ArrayList<Review>> reviews = reviewRepository.fetchUserDetailsBatches(filterTransaction(queryParams, userID));
return Helpers.writeToExcel(reviews);
}
有一个地方我一直在我的数据库配置中调用不同的 class。将其更改为适当的 class 类型解决了问题
我对特定的 class Helpers.java 进行了一些更改,每当我尝试对特定端点进行更改时,我都会收到此错误消息。堆栈跟踪指向助手的 class.
java.lang.ClassCastException: class com.clever.bas.model.Transaction cannot be cast to class com.clever.bas.model.Review (com.clever.bas.model.Transaction and com.clever.bas.model.Review are in unnamed module of loader 'app')
这是助手 class 的样子
public static InputStream writeToExcel(CompletableFuture<ArrayList<Review>> data) throws Exception {
//Blank workbook
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("transaction report");
AtomicInteger rowNum = new AtomicInteger(1);
AtomicBoolean isHeaderSet = new AtomicBoolean(false);
data.get().forEach(review -> {
if (!isHeaderSet.get()){
createHeader(review, sheet);
isHeaderSet.set(true);
}
XSSFRow row = sheet.createRow(rowNum.getAndIncrement());
ObjectMapper mapObject = new ObjectMapper();
Map<String, Object> mapObj = mapObject.convertValue(review, Map.class);
AtomicInteger cellNum = new AtomicInteger();
mapObj.forEach((key, value) -> {
XSSFCell cell = row.createCell(cellNum.getAndIncrement());
cell.setCellValue(key);
if (value instanceof Integer)
cell.setCellValue((Integer) value);
else if (value instanceof BigDecimal)
cell.setCellValue(((BigDecimal) value).doubleValue());
else if (value instanceof Long)
cell.setCellValue((Long) value);
else if (value instanceof Boolean)
cell.setCellValue((Boolean) value);
else cell.setCellValue((String) value);
});
});
try {
//Write the workbook in file system
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
byte[] barray = bos.toByteArray();
InputStream is = new ByteArrayInputStream(barray);
// InputStream is = new FileInputStream("transaction.xlsx");
// workbook.write(is);
System.out.println("transaction.xlsx written successfully on disk.");
return is;
// out.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
private static void createHeader(Review review, XSSFSheet sheet) {
XSSFRow row = sheet.createRow(0);
ObjectMapper mapObject = new ObjectMapper();
Map<String, Object> mapObj = mapObject.convertValue(review, Map.class);
AtomicInteger cellNum = new AtomicInteger();
mapObj.forEach((key, value) -> {
XSSFCell cell = row.createCell(cellNum.getAndIncrement());
cell.setCellValue(key);
});
}
我想不出我返回的是交易类型而不是评论。这是我在我的服务中调用助手 class 的地方。
public InputStream downloadReviewReport(UriInfo uriInfo) throws Exception {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String userID = queryParams.getFirst("userID");
CompletableFuture<ArrayList<Review>> reviews = reviewRepository.fetchUserDetailsBatches(filterTransaction(queryParams, userID));
return Helpers.writeToExcel(reviews);
}
有一个地方我一直在我的数据库配置中调用不同的 class。将其更改为适当的 class 类型解决了问题