如何将stacktrace作为响应从spring控制器发送为JSON Object

时间:2015-03-30 11:42:38

标签: java json spring

我想将stacktrace作为JSON对象发送到前面。例如下面是控制器

@RequestMapping("/booltest")
@ResponseBody
public String booltest(String no)
{jsonObject = new JSONObject();
try{
    int i=1/0;
    jsonObject.accumulate("Success","success");
}
catch (Exception e) {
    e.printStackTrace();
    jsonObject.accumulate("Error",e);
    // TODO: handle exception
}
    return jsonObject.toString();
}

现在当一个请求来到这个控制器然后它抛出错误,后面将是stacktrace

java.lang.ArithmeticException: / by zero
    at ServiceController.booltest(ServiceController.java:2677)
    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 org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:83)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.traceNextValve(HttpRequestOperationCollectionValve.java:112)
    at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:94)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我的要求是将整个堆栈跟踪作为对前端的响应发送。

我的方式(jsonObject.accumulate("Error",e);)输出为http://pastie.org/10062495

但我希望输出为

http://pastie.org/10062497

{"Error":"java.lang.ArithmeticException: / by zeroat com.iconma.carz.controllers.CustomerServiceController.booltest(ServiceController.java:2677)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\tat sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:83)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.traceNextValve(HttpRequestOperationCollectionValve.java:112)at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:94)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)at java.lang.Thread.run(Unknown Source)+"}

4 个答案:

答案 0 :(得分:4)

我认为您别无选择,只能处理Exception堆栈跟踪并以您需要的方式对其进行格式化。您可以通过某种方式实现打印机接口来使用printStackTrace。这样做的好处是它看起来像控制台中通常的堆栈跟踪。但是,如果您浏览堆栈跟踪元素并使用getStackTrace然后逐个处理来构建字符串,则可能会更容易。

如果你想要你甚至可以使用Apache Commons(commons-lang)并且有一个getStackTrace可以直接转换为String

String stackTrace = ExceptionUtils.getStackTrace(exception);

答案 1 :(得分:2)

您需要将堆栈跟踪收集到String中,然后将其放入jsonObject:

catch (Exception e) {
    StringWriter sw=new StringWriter();
    e.printStackTrace(new PrintWriter(sw));
    jsonObject.accumulate("Error",sw.toString);
    // TODO: handle exception
}
return jsonObject.toString();
}

答案 2 :(得分:0)

Exception提供getStackTrace()方法,只是为了以编程方式获取该信息而返回StackTraceElement[]

StackTraceElement包含getLineNumbergetFileNamegetMethodName等方法....

答案 3 :(得分:0)

catch (Exception e) {
    StackTraceElement[] stack = e.getStackTrace();
    String exception = "";
    for (StackTraceElement s : stack) {
        exception = exception + s.toString() + "\t";
    }
    jsonObject.put("Error",exception );
}