我自己实现IAccessTokenCache接口,把token存在数据库中,我是通过序列化成JSON字符串存到数据库的,存入的时候没问题。
但是取出来的时候,反序列化成一个泛型对象是有问题的(这个应该是java泛型实现的不好),泛型反序列化的时候,java的类型参数作为另一个方法的参数好像无效....
public class MySqlAccessTokenCache implements IAccessTokenCache {
    private String keyPre = "WX_";
    @Override
    public <T> T get(String s) {
        DictCache dictCache = DictCacheService.instance.get(keyPre + s);
        if (dictCache != null) {
            //******************************************************************
            //这里返回的不是T类型的对象,而是fastjson里的com.alibaba.fastjson.JSONObject
            //gson也是一样,返回com.google.gson.internal.LinkedTreeMap
            //******************************************************************
            return JSON.parseObject(dictCache.getValue(), new TypeReference<T>() {});
        } else {
            return null;
        }
    }
    @Override
    public void set(String s, Object o) {
        String json = JSON.toJSONString(o);
        DictCache dictCache = new DictCache();
        dictCache.setKey(keyPre + s);
        dictCache.setValue(json);
        DictCacheService.instance.set(dictCache);
    }
    @Override
    public void remove(String s) {
        DictCacheService.instance.remove(keyPre+s);
    }
}我感觉你是要用这个接口同时存取AccessToken和SnsAccessToken,所以我也不能在反序列化的时候把T写AccessToken或SnsAccessToken