Servlet中的异常消息

时间:2013-12-03 19:11:02

标签: java json exception servlets json-rpc

我有JSON-RPC服务(使用Reflection调用方法的类),我想将异常作为数据传递给错误对象。所以我尝试在Servlet中捕获Exception,但是e.getMessage()返回null,显示backtrace根本不显示我的代码(只有获取Stack跟踪的函数和我调用它的地方):

public Object loadService() throws InstantiationException, ClassNotFoundException, IllegalAccessException {
    ClassLoader parentClassLoader = ServiceReloader.class.getClassLoader();
    ServiceReloader classLoader = new ServiceReloader(parentClassLoader);
    //ClassLoader classLoader = init.class.getClassLoader();
    Class aClass = classLoader.loadClass("pl.jcubic.Service");
    return aClass.newInstance();
}
public String backTrace() {
    StackTraceElement[] stack = (new Throwable()).getStackTrace();
    String trace = "";
    for (int i=0; i<stack.length; ++i) {
        trace += stack[i].toString() + "\n";
    }
    return trace;
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Object id = null;
    try {
        JSONRPC2Request reqIn = JSONRPC2Request.parse(this.getInputData(request));
        id = reqIn.getID();

        Object service = loadService();

        Method[] methods = service.getClass().getMethods();

        Object result = method.invoke(service, params);
        JSONRPC2Response respOut = new JSONRPC2Response(result, id);
        out.println(respOut);

    } catch (InvocationTargetException e) {
        out.println(internalError(id, e.getMessage(), backTrace()));
    }
}

当一个方法抛出像throw new Exception("Error occured");这样的异常(我希望使用JSON-RPC发送的消息)时,我得到了这个回溯,

init.stackTrace(init.java:65)
init.doPost(init.java:119)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:724)

有没有办法显示我抛出的异常?

1 个答案:

答案 0 :(得分:0)

我在这里有两个错误:

  1. 我使用新的Trowable来创建回溯我应该使用现有的异常:

    public String backTrace(Throwable e) {
        StackTraceElement[] stack = e.getStackTrace();
        String trace = "";
        for (int i=0; i<stack.length; ++i) {
            trace += stack[i].toString() + "\n";
        }
        return trace;
    }
    
  2. 我需要使用Throwable的getCause()方法来获得真正的异常(不知道我是否只需要因为反射而使用它):

    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        out.println(internalError(id, cause.getMessage(), backTrace(cause)));
    }