ClassCastException : Integer → Long

EHOI·2022년 9월 21일

토큰에서 유저의 id를 가져와서 파라미터로 넘겨주려고 코드를 짜다가 아래와 같은 오류가 났다.

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

기본형 타입이 아니라 래퍼 클래스에서 위와 같은 에러가 났다.

구글링을 해본 결과

스택오버플로우 링크

public static Long getUserId(String token) {
    return ((Number) getAllClaims(token).get("id")).longValue();
}

잘 해결되었다!

IntegerLong 모두 Number의 하위 클래스이기 때문에

IntegerNumber로 형변환한 후에 .longValue() 로 값을 바꿔주면 된다!

profile
#성장 #단단함 #평온함

0개의 댓글