title: Spring Boot replace ServletException response in Filter
body: <p>I have a Spring Boot Filter that I'm using to authenticate using <code>Jwt</code>. If successful, everything works great and I send out a <code>Json</code> response of my design. However, if the <code>Authorization</code> header is missing or incorrect, I throw a <code>ServletException</code> with a custom message. This results in an ugly <code>Json</code> that looks like this:</p>
  
  <pre><code>{
     "timestamp":1453192910756,
     "status":500,
     "error":"Internal Server Error",
     "exception":"javax.servlet.ServletException",
     "message":"Invalid Authorization header.",
     "path":"/api/test"
  }
  </code></pre>
  
  <p>I wish to customize this <code>Json</code> so it takes the standard form I'm using for all my other responses.</p>
  
  <p>My Filter code is here:</p>
  
  <pre><code>public class JwtFilter extends GenericFilterBean {
      @Override
      public void doFilter(final ServletRequest req,
                           final ServletResponse res,
                           final FilterChain chain) throws IOException, ServletException {
          System.out.println("JwtFilter");
          final HttpServletRequest request = (HttpServletRequest) req;
          final String authHeader = request.getHeader("Authorization");
          if (authHeader == null) {
              throw new ServletException("Missing Authorization header.");
          }
          if (!authHeader.startsWith("Bearer ")) {
              throw new ServletException("Invalid Authorization header.");
          }
          final String token = authHeader.substring(7);
          try {
              final Claims claims = Jwts.parser().setSigningKey("secretkey")
                      .parseClaimsJws(token).getBody();
              request.setAttribute("claims", claims);
          }
          catch (final SignatureException e) {
              throw new ServletException("Invalid token.");
          }
          chain.doFilter(req, res);
      }
  }
  </code></pre>
  
  <p>I tried using a wrapper to wrap the response but that didn't work. Another SO post said the response was not changeable but that wouldn't even make sense. </p>
  
  <p>I think the correct way would be to edit the <code>ServletResponse</code> <code>res</code> but I couldn't get it to work.</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT:</strong> Kind of hacky but it works. If there's a better way, please answer:</p>
  
  <pre><code>public class JwtFilter extends GenericFilterBean {
      @Override
      public void doFilter(final ServletRequest req,
                           final ServletResponse res,
                           final FilterChain chain) throws IOException, ServletException {
          System.out.println("JwtFilter");
          final HttpServletRequest request = (HttpServletRequest) req;
          final String authHeader = request.getHeader("Authorization");
          if (authHeader == null) {
              res.setContentType("application/json;charset=UTF-8");
              res.getWriter().write(ExceptionCreator.createJson("Missing Authorization header."));
              return;
          }
          if (!authHeader.startsWith("Bearer ")) {
              res.setContentType("application/json;charset=UTF-8");
              res.getWriter().write(ExceptionCreator.createJson("Invalid Authorization header."));
              return;
          }
          final String token = authHeader.substring(7);
          try {
              final Claims claims = Jwts.parser().setSigningKey("secretkey")
                      .parseClaimsJws(token).getBody();
              request.setAttribute("claims", claims);
          }
          catch (Exception f) {
              res.setContentType("application/json;charset=UTF-8");
              res.getWriter().write(ExceptionCreator.createJson("Invalid token."));
              return;
          }
          chain.doFilter(req, res);
      }
  }
  </code></pre>