NutzCN Logo
精华 MethodInterceptor获取RequestBody的Json数据问题
发布于 3268天前 作者 keniushadu 3022 次浏览 复制 上一个帖子 下一个帖子
标签:

MethodInterceptor 实现代码如下:
'''
HttpServletRequest req = Mvcs.getReq();
NutMap fromJson = Json.fromJson(NutMap.class, req.getReader());
String string = fromJson.getString("test");
'''

Controller 实现代码如下
'''
@At("/api/system")
@AdaptBy(type = JsonAdaptor.class)
@IocBean
@Filters()
public class TestController {

@At("/time")
@Ok("json")
public Object time(@Param("..") AuthInfo auth, @Param("test") String test) {
    HashMap<String, Object> haha = new HashMap<>();
    haha.put("testResult", test);
    haha.put("code", 0);
    haha.put("osv", auth.get_osv());
    return haha;
}

}
'''

异常如下:
'''
17:54:13.898 [http-bio-8090-exec-8] INFO c.m.report.common.LogTimeProcessor - [POST] ReqUri= /ROOT/api/system/time,userName : null ,reqParam : [{
"_time" :0,
"_userId" :0
}, "test1"] , time : 55ms
17:54:13.903 [http-bio-8090-exec-8] WARN o.n.mvc.impl.processor.FailProcessor - Error@/api/system/time :
java.lang.IllegalStateException: getInputStream() has already been called for this request
at org.apache.catalina.connector.Request.getReader(Request.java:1249) ~[catalina.jar:7.0.65]
at org.apache.catalina.connector.RequestFacade.getReader(RequestFacade.java:505) ~[catalina.jar:7.0.65]
at com.myxiu.report.common.TestInterceptor.filter(TestInterceptor.java:35) ~[classes/:na]
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60) ~[nutz-1.b.53.jar:na]
at com.myxiu.report.controller.TestController$$NUTZAOP.time(TestController.java) ~[nutz-1.b.53.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_80]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_80]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_80]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.7.0_80]
at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.b.53.jar:na]
at com.myxiu.report.common.ValidatorProcessor.process(ValidatorProcessor.java:24) ~[classes/:na]
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:40) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:113) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.b.53.jar:na]
at com.myxiu.report.common.LogTimeProcessor.process(LogTimeProcessor.java:27) ~[classes/:na]
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:40) ~[nutz-1.b.53.jar:na]
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67) [nutz-1.b.53.jar:na]
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31) [nutz-1.b.53.jar:na]
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:183) [nutz-1.b.53.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.65]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.65]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) [catalina.jar:7.0.65]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) [catalina.jar:7.0.65]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) [catalina.jar:7.0.65]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) [catalina.jar:7.0.65]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) [catalina.jar:7.0.65]
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) [catalina.jar:7.0.65]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) [catalina.jar:7.0.65]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) [catalina.jar:7.0.65]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) [tomcat-coyote.jar:7.0.65]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) [tomcat-coyote.jar:7.0.65]
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) [tomcat-coyote.jar:7.0.65]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.7.0_80]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.7.0_80]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-coyote.jar:7.0.65]
at java.lang.Thread.run(Unknown Source) [na:1.7.0_80]

'''

请问有什么好的在MethodInterceptor 中获取请求体中JSON数据的办法么?

5 回复

有点不好弄,因为原始的req对象只能调用getReader或getInputStream一次.

我想到一个版本

既然是JsonAdaptor读取的,那么,继承一下

public MyJsonAdaptor extends JsonAdaptor {
    public Object getReferObject(    ServletContext sc,
                            HttpServletRequest req,
                            HttpServletResponse resp, String[] pathArgs) {
	   Object obj = super.getReferObject(sc, req, resp, pathsArgs);
	   req.setAttribute("_json", obj);
	   return obj;
	}
}

这样,在拦截器里面取出_json不就可以了?

换个思路
你是不是写个 Adaptor 类来解决这个问题?

@Rekoe
@wendal
thks

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