public AjaxResult save( @RequestBody CompanyAuctionDetailDTO dto) { LoginUser loginUser = getLoginUser(); CompanyAuctionDetailDTO sourceData = auctionService.getCompanyAuctionDetail(dto.getCompanyId()); compareEntities(sourceData,dto); return toAjax(auctionService.updateCompanyAuction(dto,loginUser)); } public boolean compareEntities(Entity entity1, Entity entity2) { //获取需要对比的两个实体对象 for (Field field : Entity.class.getDeclaredFields()) { // 使用for循环遍历实体的属性 field.setAccessible(true); try { Object value1 = field.get(entity1); Object value2 = field.get(entity2); if (!Objects.equals(value1, value2)) { //对比每一个属性的值是否相等 return false; } } catch (IllegalAccessException e) { e.printStackTrace(); } } return true; } 代码报错,优化
时间: 2024-02-10 07:15:03 浏览: 26
这段代码的具体报错信息没有给出,所以很难判断问题出在哪里。但是,可以在代码中添加一些异常处理来定位问题。
首先,可以在“compareEntities”方法中添加一个判断,如果参数为null,则直接返回false。此外,可以在“save”方法中添加一个判断,如果“sourceData”为null,则直接返回错误信息。
另外,需要确认实体类“Entity”是否正确导入。
下面是优化后的代码:
```
public AjaxResult save(@RequestBody CompanyAuctionDetailDTO dto) {
LoginUser loginUser = getLoginUser();
CompanyAuctionDetailDTO sourceData = auctionService.getCompanyAuctionDetail(dto.getCompanyId());
if (sourceData == null) {
return AjaxResult.error("获取拍卖公司详细信息失败");
}
if (!compareEntities(sourceData, dto)) {
return AjaxResult.error("拍卖公司详细信息有误");
}
return toAjax(auctionService.updateCompanyAuction(dto, loginUser));
}
public boolean compareEntities(Entity entity1, Entity entity2) {
if (entity1 == null || entity2 == null) {
return false;
}
// 获取需要对比的两个实体对象
for (Field field : Entity.class.getDeclaredFields()) {
// 使用for循环遍历实体的属性
field.setAccessible(true);
try {
Object value1 = field.get(entity1);
Object value2 = field.get(entity2);
if (!Objects.equals(value1, value2)) {
// 对比每一个属性的值是否相等
return false;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return true;
}
```