title: How to prevent JAX.RS ExceptionMapper including the stacktrace
body: <p>I have the following code in my JAX.RS ExceptionMapper:</p>
  
  <pre><code>@Override
  public Response toResponse(ResponseStatusCodeException ex) {
      ResponseStatusCode newEx = new ResponseStatusCode(ex);
      return Response.status(ex.getStatus()).entity(newEx).type(MediaType.APPLICATION_JSON).build();
  }
  </code></pre>
  
  <p>This generates a JSON response that includes a full exception stack trace starting in the Response.status() line.</p>
  
  <p>How can I make the ExceptionMapper just to return the JSON encoded properties for the newEx class. I have even tried making ResponseStatusCode a POJO not extending Exception but the stack trace is always included in the response.</p>
  
  <p>I am using Websphere Liberty profile 8.5.5.8.</p>
  
  <p>Thanks</p>
  
  <p>Edited: Let me add the JSON generated:</p>
  
  <pre><code>{
      "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": [
      ]
  }
  </code></pre>
  
  <p>And the POJO:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>