你好我是辰兮,本次是项目遇到的java.lang.Integer cannot be cast to java.lang.Long异常以及相对应的解决方案。
文章目录
一、实战问题
用postman测试数据报错,类型转换异常!
如何将Integer类型转换成长整形 ?
先转成String型,再转Long;
1、转String型:A.toString
2、再由String型转Long 型即可
方法1: long B = Long.valueOf("A.toString");
方法2: long B = Long.parseLong("A.toString");
案例:用json串来传值
@PostMapping("/updateLike")
public CommonResponse updateLike( @RequestBody HashMap map) {
Long postId = Long.valueOf(map.get("postId").toString());
Long userId = Long.valueOf(map.get("userId").toString());
Integer likeStatus = (Integer) map.get("likeStatus");
return CommonResponse.success(postReviewService.updateLike(reviewId,userId,likeStatus));
}
再次用postman测试
二、源码学习
底层源码分析学习
public static Long valueOf(String s) throws NumberFormatException
{
return Long.valueOf(parseLong(s, 10));
}
我们发现valueOf的底层还是parseLong
public static long parseLong(String s) throws NumberFormatException {
return parseLong(s, 10);
}
小结:先转成String型,再转Long;
Hope that we can grow and progress as soon as possible and become an excellent Java Development Engineer.
标签:lang,java,String,parseLong,valueOf,Long,toString,Integer
来源: https://blog.csdn.net/weixin_45393094/article/details/106576241