[英]Why Long.valueOf(stateMap.get(“time”)) when using Base64 encrytion giving java.lang.ClassCastException
For understanding purpose I am combining three different java file code in on Test.java file below. 为了理解目的,我在下面的Test.java文件中组合了三种不同的Java文件代码。 Line long tokenTime = Long.valueOf(decodedMap.get("time"));
long tokenTime = Long.valueOf(decodedMap.get("time"));
giving exception. 给予例外。
public class Test {
public static void main(String[] args) {
Map<String, Long> stateMap = new HashMap<>();
stateMap.put("userId",123L);
stateMap.put("resId", 456L);
stateMap.put("time", System.currentTimeMillis());
String encodedString = jsonBase64Encode(stateMap);
Map<String, Long> decodedMap = jsonBase64Decode(encodedString);
long tokenTime = Long.valueOf(decodedMap.get("time"));
System.out.println(tokenTime);
}
@SuppressWarnings("unchecked")
public static String jsonBase64Encode(Map<String, Long> stateMap){
if(stateMap == null)
return "";
JSONObject jsonObject = new JSONObject();
for( String key : stateMap.keySet() ){
jsonObject.put(key, stateMap.get(key).toString());
}
byte[] jsonBytes = jsonObject.toString().getBytes();
try {
return Base64.getEncoder().encodeToString(jsonBytes);
} catch (Exception e) {
return "";
}
}
public static Map<String, Long> jsonBase64Decode(String state)
{
if(StringUtils.isBlank(state))
return null;
String decodedStr = null;
try {
byte[] decodedBytes = Base64.getDecoder().decode(state);
decodedStr = new String(decodedBytes);
} catch (Exception e) {
return null;
}
if(StringUtils.isEmpty(decodedStr))
return null;
Map<String, Long> stateMap = new Gson().fromJson(decodedStr,
new TypeToken<HashMap<String, Object>>() {}.getType());
return stateMap;
}
} }
I am getting below error. 我正在错误以下。
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at com.test.document.utils.Test.main(Test.java:26)
That means your runtime map is returning a string when your code was compiled with it expecting a Long: 这意味着您的运行时映射在编译代码时返回一个字符串,该字符串期望Long:
decodedMap.get("time") //At compile time, this is a Long
But when the code runs at runtime, the actual value is a String. 但是,当代码在运行时运行时,实际值为String。
You can replicate it easily by pushing a string into the map: 您可以通过将字符串推入映射轻松地复制它:
stateMap.put("resId", 456L);
((Map) stateMap).put("time", String.valueOf(System.currentTimeMillis()));
long tokenTime = Long.valueOf(stateMap.get("time"));
The last line above raises: 上面的最后一行提出了:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long 线程“主”中的异常java.lang.ClassCastException:无法将java.lang.String强制转换为java.lang.Long
Your JSON needs to be fixed, or you need to change your code to compile against Long.valueOf(String)
您的JSON需要修复,或者您需要更改代码以针对Long.valueOf(String)
进行编译
Change 更改
Map<String, Long> stateMap = new Gson().fromJson(decodedStr, new TypeToken<HashMap<String, Object>>() {}.getType());
to 至
Map<String, Long> stateMap = new Gson().fromJson(decodedStr, new TypeToken<HashMap<String, Long>>() {}.getType());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.