我在JAX.RS ExceptionMapper下面的代碼:如何防止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();
}
這產生包括一個完整的異常堆棧跟蹤在Response.status開始JSON響應()線。
如何讓ExceptionMapper只返回newEx類的JSON編碼屬性。我甚至嘗試使ResponseStatusCode成爲POJO而不是擴展異常,但堆棧跟蹤始終包含在響應中。
我正在使用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;
}
}
屬性是使用一般干將連載。你有異常的吸氣劑或pojo內的東西? –
謝謝。我只包含了POJO,它沒有任何異常信息,所以我不知道生成的JSON中的堆棧跟蹤來自哪裏。 – icordoba
您正在繼承超類'Exception/Throwable'的所有獲取者 –