如何防止包含stacktrace的JAX.RS ExceptionMapper

时间:2015-12-31 11:40:26

标签: exception-handling jax-rs

我的JAX.RS ExceptionMapper中有以下代码:

@Override
public Response toResponse(ResponseStatusCodeException ex) {
    ResponseStatusCode newEx = new ResponseStatusCode(ex);
    return Response.status(ex.getStatus()).entity(newEx).type(MediaType.APPLICATION_JSON).build();
}

这将生成一个JSON响应,其中包含从Response.status()行开始的完整异常堆栈跟踪。

如何让ExceptionMapper只返回newEx类的JSON编码属性。我甚至尝试使ResponseStatusCode成为POJO而不是扩展Exception,但堆栈跟踪始终包含在响应中。

我正在使用Websphere Liberty配置文件8.5.5.8。

由于

编辑:让我添加生成的JSON:

{
    "message": "Language en is not currently available",
    "status": 500,
    "username": "901353fa-b43e-4e86-b12f-6d502a9e8a78",
    "localizedMessage": "Language en is not currently available",
    "stackTrace": [
        {
            "fileName": "ResponseStatusCodeExceptionHandler.java",
            "lineNumber": 14,
            "className": "com.servengine.ws.rs.ResponseStatusCodeExceptionHandler",
            "methodName": "toResponse",
            "nativeMethod": false
        },
        {
            "fileName": "ResponseStatusCodeExceptionHandler.java",
            "lineNumber": 1,
            "className": "com.servengine.ws.rs.ResponseStatusCodeExceptionHandler",
            "methodName": "toResponse",
            "nativeMethod": false
        },
        {
            "fileName": "ExceptionUtils.java",
            "lineNumber": 90,
            "className": "org.apache.cxf.jaxrs.utils.ExceptionUtils",
            "methodName": "convertFaultToResponse",
            "nativeMethod": false
        },
        {
            "fileName": "JAXRSUtils.java",
            "lineNumber": 1621,
            "className": "org.apache.cxf.jaxrs.utils.JAXRSUtils",
            "methodName": "convertFaultToResponse",
            "nativeMethod": false
        },
        {
            "fileName": "JAXRSInvoker.java",
            "lineNumber": 324,
            "className": "org.apache.cxf.jaxrs.JAXRSInvoker",
            "methodName": "handleFault",
            "nativeMethod": false
        },
        {
            "fileName": "JAXRSInvoker.java",
            "lineNumber": 213,
            "className": "org.apache.cxf.jaxrs.JAXRSInvoker",
            "methodName": "invoke",
            "nativeMethod": false
        },
        {
            "fileName": "LibertyJaxRsInvoker.java",
            "lineNumber": 366,
            "className": "com.ibm.ws.jaxrs20.server.LibertyJaxRsInvoker",
            "methodName": "invoke",
            "nativeMethod": false
        },
        {
            "fileName": "JAXRSInvoker.java",
            "lineNumber": 99,
            "className": "org.apache.cxf.jaxrs.JAXRSInvoker",
            "methodName": "invoke",
            "nativeMethod": false
        },
        {
            "fileName": "ServiceInvokerInterceptor.java",
            "lineNumber": 59,
            "className": "org.apache.cxf.interceptor.ServiceInvokerInterceptor$1",
            "methodName": "run",
            "nativeMethod": false
        },
        {
            "fileName": "ServiceInvokerInterceptor.java",
            "lineNumber": 96,
            "className": "org.apache.cxf.interceptor.ServiceInvokerInterceptor",
            "methodName": "handleMessage",
            "nativeMethod": false
        },
        {
            "fileName": "PhaseInterceptorChain.java",
            "lineNumber": 307,
            "className": "org.apache.cxf.phase.PhaseInterceptorChain",
            "methodName": "doIntercept",
            "nativeMethod": false
        },
        {
            "fileName": "ChainInitiationObserver.java",
            "lineNumber": 124,
            "className": "org.apache.cxf.transport.ChainInitiationObserver",
            "methodName": "onMessage",
            "nativeMethod": false
        },
        {
            "fileName": "AbstractHTTPDestination.java",
            "lineNumber": 256,
            "className": "org.apache.cxf.transport.http.AbstractHTTPDestination",
            "methodName": "invoke",
            "nativeMethod": false
        },
        {
            "fileName": "AbstractJaxRsWebEndpoint.java",
            "lineNumber": 134,
            "className": "com.ibm.ws.jaxrs20.endpoint.AbstractJaxRsWebEndpoint",
            "methodName": "invoke",
            "nativeMethod": false
        },
        {
            "fileName": "IBMRestServlet.java",
            "lineNumber": 149,
            "className": "com.ibm.websphere.jaxrs.server.IBMRestServlet",
            "methodName": "handleRequest",
            "nativeMethod": false
        },
        {
            "fileName": "IBMRestServlet.java",
            "lineNumber": 107,
            "className": "com.ibm.websphere.jaxrs.server.IBMRestServlet",
            "methodName": "doPost",
            "nativeMethod": false
        },
        {
            "fileName": "HttpServlet.java",
            "lineNumber": 707,
            "className": "javax.servlet.http.HttpServlet",
            "methodName": "service",
            "nativeMethod": false
        },
        {
            "fileName": "IBMRestServlet.java",
            "lineNumber": 99,
            "className": "com.ibm.websphere.jaxrs.server.IBMRestServlet",
            "methodName": "service",
            "nativeMethod": false
        },
        {
            "fileName": "ServletWrapper.java",
            "lineNumber": 1287,
            "className": "com.ibm.ws.webcontainer.servlet.ServletWrapper",
            "methodName": "service",
            "nativeMethod": false
        },
        {
            "fileName": "ServletWrapper.java",
            "lineNumber": 778,
            "className": "com.ibm.ws.webcontainer.servlet.ServletWrapper",
            "methodName": "handleRequest",
            "nativeMethod": false
        },
        {
            "fileName": "ServletWrapper.java",
            "lineNumber": 475,
            "className": "com.ibm.ws.webcontainer.servlet.ServletWrapper",
            "methodName": "handleRequest",
            "nativeMethod": false
        },
        {
            "fileName": "WebAppFilterChain.java",
            "lineNumber": 146,
            "className": "com.ibm.ws.webcontainer.filter.WebAppFilterChain",
            "methodName": "invokeTarget",
            "nativeMethod": false
        },
        {
            "fileName": "WebAppFilterChain.java",
            "lineNumber": 78,
            "className": "com.ibm.ws.webcontainer.filter.WebAppFilterChain",
            "methodName": "doFilter",
            "nativeMethod": false
        },
        {
            "fileName": "WebAppFilterManager.java",
            "lineNumber": 1020,
            "className": "com.ibm.ws.webcontainer.filter.WebAppFilterManager",
            "methodName": "doFilter",
            "nativeMethod": false
        },
        {
            "fileName": "WebAppFilterManager.java",
            "lineNumber": 1142,
            "className": "com.ibm.ws.webcontainer.filter.WebAppFilterManager",
            "methodName": "invokeFilters",
            "nativeMethod": false
        },
        {
            "fileName": "CacheServletWrapper.java",
            "lineNumber": 81,
            "className": "com.ibm.ws.webcontainer.servlet.CacheServletWrapper",
            "methodName": "__handleRequest",
            "nativeMethod": false
        },
        {
            "fileName": "CacheServletWrapper.java",
            "lineNumber": -1,
            "className": "com.ibm.ws.webcontainer.servlet.CacheServletWrapper",
            "methodName": "handleRequest",
            "nativeMethod": false
        },
        {
            "fileName": "WebContainer.java",
            "lineNumber": 928,
            "className": "com.ibm.ws.webcontainer.WebContainer",
            "methodName": "handleRequest",
            "nativeMethod": false
        },
        {
            "fileName": "DynamicVirtualHost.java",
            "lineNumber": 262,
            "className": "com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$2",
            "methodName": "run",
            "nativeMethod": false
        },
        {
            "fileName": "HttpDispatcherLink.java",
            "lineNumber": 955,
            "className": "com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink$TaskWrapper",
            "methodName": "run",
            "nativeMethod": false
        },
        {
            "fileName": "HttpDispatcherLink.java",
            "lineNumber": 341,
            "className": "com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink",
            "methodName": "ready",
            "nativeMethod": false
        },
        {
            "fileName": "HttpInboundLink.java",
            "lineNumber": 470,
            "className": "com.ibm.ws.http.channel.internal.inbound.HttpInboundLink",
            "methodName": "handleDiscrimination",
            "nativeMethod": false
        },
        {
            "fileName": "HttpInboundLink.java",
            "lineNumber": 404,
            "className": "com.ibm.ws.http.channel.internal.inbound.HttpInboundLink",
            "methodName": "handleNewRequest",
            "nativeMethod": false
        },
        {
            "fileName": "HttpInboundLink.java",
            "lineNumber": 284,
            "className": "com.ibm.ws.http.channel.internal.inbound.HttpInboundLink",
            "methodName": "processRequest",
            "nativeMethod": false
        },
        {
            "fileName": "HttpInboundLink.java",
            "lineNumber": 255,
            "className": "com.ibm.ws.http.channel.internal.inbound.HttpInboundLink",
            "methodName": "ready",
            "nativeMethod": false
        },
        {
            "fileName": "NewConnectionInitialReadCallback.java",
            "lineNumber": 174,
            "className": "com.ibm.ws.tcpchannel.internal.NewConnectionInitialReadCallback",
            "methodName": "sendToDiscriminators",
            "nativeMethod": false
        },
        {
            "fileName": "NewConnectionInitialReadCallback.java",
            "lineNumber": 83,
            "className": "com.ibm.ws.tcpchannel.internal.NewConnectionInitialReadCallback",
            "methodName": "complete",
            "nativeMethod": false
        },
        {
            "fileName": "WorkQueueManager.java",
            "lineNumber": 504,
            "className": "com.ibm.ws.tcpchannel.internal.WorkQueueManager",
            "methodName": "requestComplete",
            "nativeMethod": false
        },
        {
            "fileName": "WorkQueueManager.java",
            "lineNumber": 574,
            "className": "com.ibm.ws.tcpchannel.internal.WorkQueueManager",
            "methodName": "attemptIO",
            "nativeMethod": false
        },
        {
            "fileName": "WorkQueueManager.java",
            "lineNumber": 929,
            "className": "com.ibm.ws.tcpchannel.internal.WorkQueueManager",
            "methodName": "workerRun",
            "nativeMethod": false
        },
        {
            "fileName": "WorkQueueManager.java",
            "lineNumber": 1018,
            "className": "com.ibm.ws.tcpchannel.internal.WorkQueueManager$Worker",
            "methodName": "run",
            "nativeMethod": false
        },
        {
            "fileName": "ThreadPoolExecutor.java",
            "lineNumber": 1142,
            "className": "java.util.concurrent.ThreadPoolExecutor",
            "methodName": "runWorker",
            "nativeMethod": false
        },
        {
            "fileName": "ThreadPoolExecutor.java",
            "lineNumber": 617,
            "className": "java.util.concurrent.ThreadPoolExecutor$Worker",
            "methodName": "run",
            "nativeMethod": false
        },
        {
            "fileName": "Thread.java",
            "lineNumber": 745,
            "className": "java.lang.Thread",
            "methodName": "run",
            "nativeMethod": false
        }
    ],
    "suppressed": [
    ]
}

和POJO:

package com.servengine.ws.rs;

public class ResponseStatusCode extends Exception {
    private static final long serialVersionUID = 1L;

    private int status;
    private String message, username, code;

    public ResponseStatusCode(ResponseStatusCodeException exception) {
        this.message = exception.getMessage();
        this.username = exception.getUsername();
        this.code = exception.getCode();
        this.status = exception.getStatus();
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

2 个答案:

答案 0 :(得分:0)

如果要编组为JSON的类扩展了Exception,则将包含堆栈跟踪。我将其更改为Serializable,我还没有完成,并且没有包含堆栈跟踪。

非常感谢@peeskillet的回复。

答案 1 :(得分:0)

如果您使用jackson编组POJO到JSON,您可以尝试这样做:

@JsonIgnoreProperties(value = "stackTrace")
public class ResponseStatusCode extends Exception