NutzCN Logo
问答 关于SLOG中从Nutmp中取值的问题
发布于 2730天前 作者 elkan1788 2353 次浏览 复制 上一个帖子 下一个帖子
标签:

根据SLOG的插件,稍微改写了下一些代码,然后在Module层注入遇到一个问题,因为content要从参数中取值,当这个参数为pojo实体时没有问题,可当传一个Nutmp时虽然可以正常的插入数据,但后台报一堆错误,如下:

17-06-15 19:27:03.536 INFO [qtp1725312089-23] Fail to value by getter
java.lang.IllegalArgumentException: Fail to create FastClass for javax.servlet.http.Cookie_ClassRealm[plugin>org.eclipse.jetty:jetty-maven-plugin:9.4.0.v20161208, parent: sun.misc.Launcher$AppClassLoader@18b4aac2]
	at org.nutz.lang.reflect.FastClassFactory.get(FastClassFactory.java:51)
	at org.nutz.lang.reflect.FastClassFactory.get(FastClassFactory.java:59)
	at org.nutz.lang.eject.EjectByGetter.eject(EjectByGetter.java:32)
	at org.nutz.json.entity.JsonEntityField.getValue(JsonEntityField.java:167)
	at org.nutz.json.impl.JsonRenderImpl.pojo2Json(JsonRenderImpl.java:262)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:133)
	at org.nutz.json.impl.JsonRenderImpl.array2Json(JsonRenderImpl.java:421)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:129)
	at org.nutz.json.impl.JsonRenderImpl.appendPair(JsonRenderImpl.java:172)
	at org.nutz.json.impl.JsonRenderImpl.writeItem(JsonRenderImpl.java:339)
	at org.nutz.json.impl.JsonRenderImpl.pojo2Json(JsonRenderImpl.java:332)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:133)
	at org.nutz.json.impl.JsonRenderImpl.appendPair(JsonRenderImpl.java:172)
	at org.nutz.json.impl.JsonRenderImpl.writeItem(JsonRenderImpl.java:339)
	at org.nutz.json.impl.JsonRenderImpl.pojo2Json(JsonRenderImpl.java:332)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:133)
	at org.nutz.json.impl.JsonRenderImpl.array2Json(JsonRenderImpl.java:417)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:129)
	at org.nutz.json.impl.JsonRenderImpl.appendPair(JsonRenderImpl.java:172)
	at org.nutz.json.impl.JsonRenderImpl.writeItem(JsonRenderImpl.java:339)
	at org.nutz.json.impl.JsonRenderImpl.map2Json(JsonRenderImpl.java:226)
	at org.nutz.json.impl.JsonRenderImpl.render(JsonRenderImpl.java:121)
	at org.nutz.json.Json.toJson(Json.java:239)
	at org.nutz.json.Json.toJson(Json.java:201)
	at org.nutz.lang.util.SimpleContext.toString(SimpleContext.java:64)
	at io.github.elkan1788.groupbuying.interceptor.SLogAopInterceptor.filter(SLogAopInterceptor.java:68)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
	at io.github.elkan1788.groupbuying.modules.admin.ImageLibraryModule$$NUTZAOP.ajaxUploadAd(ImageLibraryModule.java:1)
	at io.github.elkan1788.groupbuying.modules.admin.ImageLibraryModule$FM$ajaxUploadAd$542b97053142f1e4dccfdb04468ae5a6.invoke(ImageLibraryModule.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1613)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:541)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1584)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1228)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:481)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1553)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1130)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:118)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.Server.handle(Server.java:564)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:318)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:112)
	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
	at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:122)
	at org.eclipse.jetty.util.thread.strategy.ExecutingExecutionStrategy.invoke(ExecutingExecutionStrategy.java:58)
	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:201)
	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:133)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)

Module层的注解及代码如下:

 @At
    @AdaptBy(type = UploadAdaptor.class, args = { "ioc:uploadImg" })
    @SLog(module=Constants.MODULE_SHOP, type=Constants.OPERATE_TYPE_ADD, content="更新商城首页广告图片【${args[0]['orderNo']}】信息")
    public AjaxReturn ajaxUploadAd(@Param("..")NutMap nm, HttpServletRequest req, ServletContext context) {
        AjaxReturn ar = Ajax.ok();
        String savePath = props.get(Constants.UPLOAD_ATTACHMENT_SAVE_PATH) + props.get(Constants.SHOP_AD_FOLDER);
        ImageLibrary il = imgLibService.uploadShopAdImg(nm, savePath);
        ar.setData(il);
        return ar;
    }

SLOG拦截代码:

public class SLogAopInterceptor implements MethodInterceptor {

    private static final Log logger = Logs.get();

    protected AdminLogService operateLogService;

    protected String remarks;
    protected String module;
    protected String operateType;
    protected CharSegment content;
    protected Map<String, El> els = new HashMap<>();

    public SLogAopInterceptor(Ioc ioc, SLog slog, Method method) {

        this.content = new CharSegment(slog.content());
        if (content.hasKey()) {
            els.clear();
            for (String key : content.keys()) {
                els.put(key, new El(key));
            }
        }
        this.operateLogService = ioc.get(AdminLogService.class);
        this.remarks = method.getDeclaringClass().getName() + "#" + method.getName();
        this.module = slog.module();
        this.operateType = slog.type();
    }

    public void filter(InterceptorChain chain) throws Throwable {
        try {
            chain.doChain();
            String _content;
            if (content.hasKey()) {
                Context ctx = Lang.context();
                ctx.set("args", chain.getArgs());
                ctx.set("req", Mvcs.getReq());
                ctx.set("return", chain.getReturn());
                Context _ctx = Lang.context();
                // 错误就出在这个循环里面
                for (String key : content.keys()) {
                    _ctx.set(key, els.get(key).eval(ctx));
                }
                _content = content.render(_ctx).toString();
            } else {
                _content = content.getOrginalString();
            }

            HttpSession session = Mvcs.getHttpSession(false);
            SysAdmin sysAdmin = (SysAdmin) session.getAttribute(Constants.ADMIN_LOGIN_SESSION);

            AdminLog operateLog = new AdminLog();
            operateLog.setSaId(sysAdmin.getAdminId());
            operateLog.setModule(this.module);
            operateLog.setOperateType(this.operateType);
            operateLog.setContent(_content);
            operateLog.setIpAddr(Lang.getIP(Mvcs.getReq()));
            operateLog.setRemarks(this.remarks);
            operateLog.setCreateTime(new Date());
            this.operateLogService.asyncLog(operateLog);
        } catch (Throwable e) {
           logger.error("Save admin operate log failed.", e);
        }
    }

}

有人使用过类似的做法么?

9 回复
	at org.nutz.lang.util.SimpleContext.toString(SimpleContext.java:64)
	at io.github.elkan1788.groupbuying.interceptor.SLogAopInterceptor.filter(SLogAopInterceptor.java:68)

68行是哪一行

在SLOG拦截代码中已经注释说明啦,就是下面这行:

 // 错误就出在这个循环里面
for (String key : content.keys()) {
     ctx.set(key, els.get(key).eval(ctx));
}

但报错的是toString(), 并不是你以为的那一行

里面只有一个toString(),但对象不像是SimpleContext的实例

可是这个代码就是68行的,明天再debug看看

来自 Simulator

@wendal 有点奇怪了,在创建Context就报错了,如下所示:
nutz-sylog-bug01.png
nutz-sylog-bug02.png

debug的时候, idea为了显示ctx变量的内容,对其调用了toString(), 然后SimpleContext的toString调用toJson.

然后SimpleContext实例里面有个request对象, 递归序列化成json的过程中,生成FastClass抛出异常

我得想想怎么改, 如果你急用的话,可以先禁用掉FastClass

NutConf.USE_FASTCLASS = false;

好的,确实是IDE DBUG所导致的, 暂时停掉就是啦,不急。

添加回复
请先登陆
回到顶部