为什么在使用 Base64 加密时 Long.valueOf(stateMap.get("time")) java.lang.ClassCastException
Why Long.valueOf(stateMap.get("time")) when using Base64 encrytion giving java.lang.ClassCastException
为了便于理解,我在下面的 Test.java 文件中组合了三个不同的 java 文件代码。第 long tokenTime = Long.valueOf(decodedMap.get("time"));
行给出异常。
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;
}
}
我遇到了以下错误。
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)
这意味着当您的代码在编译时期望 Long:
decodedMap.get("time") //At compile time, this is a Long
但是当代码在运行时运行时,实际值是一个字符串。
您可以通过将字符串推入映射来轻松复制它:
stateMap.put("resId", 456L);
((Map) stateMap).put("time", String.valueOf(System.currentTimeMillis()));
long tokenTime = Long.valueOf(stateMap.get("time"));
上面最后一行提出:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
您的 JSON 需要修复,或者您需要更改代码以针对 Long.valueOf(String)
进行编译
改变
Map<String, Long> stateMap = new Gson().fromJson(decodedStr,
new TypeToken<HashMap<String, Object>>() {}.getType());
到
Map<String, Long> stateMap = new Gson().fromJson(decodedStr,
new TypeToken<HashMap<String, Long>>() {}.getType());
为了便于理解,我在下面的 Test.java 文件中组合了三个不同的 java 文件代码。第 long tokenTime = Long.valueOf(decodedMap.get("time"));
行给出异常。
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;
}
}
我遇到了以下错误。
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)
这意味着当您的代码在编译时期望 Long:
decodedMap.get("time") //At compile time, this is a Long
但是当代码在运行时运行时,实际值是一个字符串。
您可以通过将字符串推入映射来轻松复制它:
stateMap.put("resId", 456L);
((Map) stateMap).put("time", String.valueOf(System.currentTimeMillis()));
long tokenTime = Long.valueOf(stateMap.get("time"));
上面最后一行提出:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
您的 JSON 需要修复,或者您需要更改代码以针对 Long.valueOf(String)
改变
Map<String, Long> stateMap = new Gson().fromJson(decodedStr,
new TypeToken<HashMap<String, Object>>() {}.getType());
到
Map<String, Long> stateMap = new Gson().fromJson(decodedStr,
new TypeToken<HashMap<String, Long>>() {}.getType());