spring的ProceedingJoinPoint如何串改参数?

我是这样写的

    public Object handle(ProceedingJoinPoint thisJoinPoint) throws Throwable {
        Object[] objects = thisJoinPoint.getArgs();
        for (int i = 0; i < objects.length; i++) {
            Object o = objects[i];
            String json = JSONObject.toJSONString(o);
            JSONObject data = JSONObject.parseObject(json);
            data.put("sex", 20);
            objects[i] = data;
        }
        System.out.println(JSONObject.toJSONString(objects));
        Object result = thisJoinPoint.proceed(objects);
        System.out.println(result);
        return result;
    }

这样data不是原来的那个Object类型。

阅读 2.5k
1 个回答
 JSONObject data = JSONObject.parseObject(json);
 data.put("sex", 20);
 objects[i] = data;

data被你改成了JSONObject 类型;
如果你想对data设置属性,建议你通过 instaceof 来判断以后进行强转后再调用setSex方法即可
例如:

if(objects[i] instanceof User){
((User) objects[i]).setSex(20)
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题