code: public static void respondAsJsonWithHttpStatus(HttpServletResponse resp, Object response, int statusCode)
              throws IOException {
          String json = new GsonBuilder().serializeNulls().create().toJson(response);
          String jsonUtf8 = new String(json.getBytes(), "UTF-8");
          resp.setContentType("application/json");
          resp.setCharacterEncoding("UTF-8");
          resp.setStatus(statusCode);
          resp.getOutputStream().print(jsonUtf8);
          resp.flushBuffer();
      }
docs: Sends a HTTP response as a application/json document and with a HTTP status code.
  
  @param resp
  A {@link HttpServletResponse} object that the servlet is responding on.
  @param response
  The response object which will be serialized to a JSON document
  @param statusCode
  The HTTP status code to send with the response
  @throws IOException