@王爵 你好,想跟你请教个问题:我使用你的blade框架开发了一个应用,当我对post请求进行json回复时老是报如下错误,但以text方式回复时是正常的,我一直找不懂原因,请你分析一下。谢谢。我使用的是blade 1.1.3版本
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at blade.kit.ReflectKit.invokeMehod(ReflectKit.java:198)
at blade.RequestHandler.executeMethod(RequestHandler.java:295)
at blade.RequestHandler.handler(RequestHandler.java:155)
at blade.BladeFilter.doFilter(BladeFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at blade.kit.ReflectKit.invokeMehod(ReflectKit.java:196)
... 21 more
Caused by: java.lang.NullPointerException
at blade.render.Render.json(Render.java:112)
at blade.servlet.Response.json(Response.java:232)
at com.ithgp.route.AssistantTool.userLogin(AssistantTool.java:96)
额,我知道原因了。
publicvoidjson(Stringjson)
{
if(null!=json){
HttpServletResponseresponse=BladeWebContext.servletResponse();
HttpServletRequestrequest=BladeWebContext.servletRequest();
response.setHeader("Cache-Control","no-cache");
StringuserAgent=request.getHeader("User-Agent");
if(userAgent.contains("MSIE"))
response.setContentType("text/html;charset=utf-8");
else
response.setContentType("application/json;charset=utf-8");
try
{
request.setCharacterEncoding("utf-8");
PrintWriterout=response.getWriter();
out.print(json.toString());
out.flush();
out.close();
}catch(UnsupportedEncodingExceptione1){
e1.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
原因是没有对userAgent做非空检查,我是用代码模拟登录的,没有写User-Agent头,所以
造成这个异常抛出的。
建议使用最新版本,官网:http://bladejava.com/文档:http://bladejava.com/docs/index.html版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。