前端封装的json格式的数据,后台接收的参数的时候直接使用的Map去接收的,导致再对数据进行forearch操作的时候报错,提示信息为
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.ruoyi.systemTestTaskManager.domain.HollySubTaskDevices
public int updateSubTaskInfo(Map<String, Object> confirmParms) {
List<HollySubTaskDevices> toolDeploys = (ArrayList<HollySubTaskDevices>) ;
confirmParms.get("devices");
if (toolDeploys.size() != 0) {
toolDeploys.forEach(obj -> obj.setRelationId(IdUtils.simpleUUID()));
toolDeploys.forEach(obj -> obj.setDelFlag("0"));
hollySubTaskMapper.updateSubTaskDevices(toolDeploys);
hollySubTaskMapper.insertSubTaskDevices(toolDeploys);
}
}
解决方案1:
ObjectMapper mapper = new ObjectMapper();
List<HollySubTaskDevices> list1 = (List<HollySubTaskDevices>) confirmParms.get("devices");
List<HollySubTaskDevices> devices= mapper.convertValue(list1, new TypeReference<List<HollySubTaskDevices>>(){});
引入包的路径
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
结果:
解决方案2:使用流的方式转换
解决方案3:后台定义一个dto 里面的数据格式与前端传递的格式一致。controller层接收时用这个dto 接收
/**
* @param confirmParms
* @return {@link AjaxResult}
* @date 2022/9/9 15:17
* @author leixin
* @Description
*/
@PostMapping("/updateSubTaskInfo")
public AjaxResult updateSubTaskInfo(@RequestBody ConfirmParmsDTO confirmParms) {
return AjaxResult.success( hollySubTaskService.updateSubTaskInfo(confirmParms));
}
ConfirmParmsDTO :
@Data
public class ConfirmParmsDTO {
private List<HollySubTaskDevices> devices;
private List<HollySubTaskToolDeploy> toolDeploy;
private List<HollySubTaskToolConfig> toolConfig;
private Map<String,Object> topoData;
}
server层处理
public int updateSubTaskInfo(ConfirmParmsDTO confirmParms) {
List<HollySubTaskDevices> toolDeploys = confirmParms.getDevices();
List<HollySubTaskToolConfig> hollySubTaskToolConfigs = confirmParms.getToolConfig();
List<HollySubTaskToolDeploy> hollySubTaskToolDeploys = confirmParms.getToolDeploy();
Map<String, Object> topoData = confirmParms.getTopoData();
if (toolDeploys.size() != 0) {
/** 关联Id*/
toolDeploys.forEach(obj -> obj.setRelationId(IdUtils.simpleUUID()));
toolDeploys.forEach(obj -> obj.setDelFlag("0"));
hollySubTaskMapper.updateSubTaskDevices(toolDeploys);
hollySubTaskMapper.insertSubTaskDevices(toolDeploys);
}
if (hollySubTaskToolConfigs.size() != 0) {
hollySubTaskToolConfigs.forEach(obj -> obj.setRelationId(IdUtils.simpleUUID()));
hollySubTaskToolConfigs.forEach(obj -> obj.setDelFlag("0"));
hollySubTaskMapper.updateSubTaskToolConfigs(hollySubTaskToolConfigs);
hollySubTaskMapper.insertHollySubTaskToolConfigs(hollySubTaskToolConfigs);
}
if (hollySubTaskToolDeploys.size() != 0) {
hollySubTaskToolDeploys.forEach(obj -> obj.setRelationId(IdUtils.simpleUUID()));
hollySubTaskToolDeploys.forEach(obj -> obj.setDelFlag("0"));
hollySubTaskMapper.updateHollySubTaskToolDeploys(hollySubTaskToolDeploys);
hollySubTaskMapper.insertHollySubTaskToolDeploys(hollySubTaskToolDeploys);
}
String subTaskId = String.valueOf(topoData.get("subTaskId"));
String jsonStr = new JSONObject(topoData).toString();
hollySubTaskMapper.updateTopoData(subTaskId,jsonStr );
return 0;
}
描述有误的地方欢迎大家指正,大家有问题可加v 876942434。一起进步~