你好我是辰兮,本次是項目遇到的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