0

我正在尝试在我的 WebApp 和磁盘上的存储之间建立代理。我知道 WebApp 一切正常,它甚至将内容保存到磁盘,所以我知道 InvocationHandler 上的 Invoke 方法被调用,但代理本身似乎为 Null,因此 WebApp 上没有任何内容得到更新。

创建代理:

public class Storage {

public static final Storage INSTANCE = new Storage();

private Storage() {

}

//private final Map<String, Note> tmpStorage = new HashMap<>();

public Map<String, Note> getStorageForUser(Credentials credentials) {
    String location = "./storage/"+credentials.getUsername();
    return (Map<String, Note>) Proxy.newProxyInstance(
            Map.class.getClassLoader(),
            new Class<?>[]{Map.class},
            new FileStorageMap(location, new HashMap<>()));
    //return tmpStorage;
}

}

调用处理程序:

    @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (method.getName() == "post" || method.getName() == "put"){
        FileUtils.writeStringToFile(new File(folder.getPath()+"/"+args[0]), (new NoteToJson((Note)args[1])).getJson().toString(), "UTF-8");
    }
    else{
        new File(folder.getPath()+"/"+args[0]).delete();
    }
    return method.invoke(proxiedMap, args);
}

我已经检查并在创建名为“FileStorageMap”的 InvocationHandler 时,文件夹和 proxiedMap 是数据字段,它们都不是 Null,它们都已正确填写。但是对于代理本身,它返回 NullPointerException。谁能告诉我这是为什么?

4

1 回答 1

0

错误在于方法名称是错误的,并且更多的东西进入了 else 语句,这超出了我的预期。代理本身并不是真正的问题。

于 2018-11-13T14:27:28.373 回答