Java类型转换报错:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer
源码如下:
Map<String, Object> map = mapList.get(0);
int type = (int)map.getOrDefault("type", 0);//此处报错,type数据库查出为3
解决办法:
先将Map的结果取出为Object,再转为int
Map<String, Object> map = mapList.get(0);
Object typeObj = map.getOrDefault("type", 0);
int type = Integer.parseInt(String.valueOf(typeObj));