Search code examples
javaservletsnetflix-zuul

Servlet's service and init method are being called, but not doGet while I am extending ZuulServlet


Before I start, I would say, I have already seen similar question Servlet's service and init method are being called, but not doGet but that dint help me much. Point to be noted here is that my servlet class extending com.netflix.zuul.http.ZuulServlet (which does routing for specific reason) and in service method I am calling super.service(servletRequest,servletResponse);.When I do http get call, I see my service() method is called but doGet() method is not called however in response I get a "HTTP/1.1 200 OK" without any body which is of course my doGet() method not sending. Below is my code snippet.

public class AppHealthServlet extends ZuulServlet {
@Override
public void init() throws ServletException {
    super.init();
    logger.info("AppHealthServlet called ");
    message = "pong";
}

@Override
public void service(ServletRequest servletRequest,
                    ServletResponse servletResponse) throws ServletException, IOException {
    logger.info("AppHealthServlet service called ");
    try {
        RequestContext.getCurrentContext().set("health", true);
        super.service(servletRequest,servletResponse);
    } finally {
        logger.info("AppHealthServlet service done ");
    }
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.doGet(req, resp);
    logger.info("AppHealthServlet doGet called ");
    // Set response content type
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    out.println("<h1>" + message + "</h1>");
    resp.setStatus(AppHealthServlet.SC_OK);
}
}

Would be appreciated if anyone can help me to figure it out where I am doing mistake.


Solution

  • It looks like ZuulServlet isn't meant to be used like this. If you look at the source code of what ZuulServlet.service does, it basically hands everything off to a ZuulRunner.

    It seems to me that you need to be looking into hooking into the Zuul routing, rather than trying to extend ZuulServlet yourself.