Class RestServlet

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
org.apache.juneau.rest.servlet.RestServlet
All Implemented Interfaces:
jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable
Direct Known Subclasses:
BasicRestServlet, SpringRestServlet

public abstract class RestServlet extends jakarta.servlet.http.HttpServlet
Servlet implementation of a REST resource.

The RestServlet class is the entry point for your REST resources. It extends directly from HttpServlet and is deployed like any other servlet.

When the servlet init() method is called, it triggers the code to find and process the @Rest annotations on that class and all child classes. These get constructed into a RestContext object that holds all the configuration information about your resource in a read-only object.

Notes:
See Also:
  • Field Summary

    Fields inherited from class jakarta.servlet.http.HttpServlet

    LEGACY_DO_HEAD
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    protected void
    doLog(Level level, Throwable cause, Supplier<String> msg)
    Main logger method.
    Returns the read-only context object that contains all the configuration information about this resource.
    Returns the path for this resource as defined by the @Rest(path) annotation or RestContext.Builder.path(String) method concatenated with those on all parent classes.
    Returns the current thread-local HTTP request.
    Returns the current thread-local HTTP response.
    void
    init(jakarta.servlet.ServletConfig servletConfig)
     
    void
    log(Level level, String msg, Object... args)
    Log a message.
    void
    log(Level level, Throwable cause, String msg, Object... args)
    Log a message.
    void
    service(jakarta.servlet.http.HttpServletRequest r1, jakarta.servlet.http.HttpServletResponse r2)
    The main service method.
    protected void
    Sets the context object for this servlet.

    Methods inherited from class jakarta.servlet.http.HttpServlet

    doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service

    Methods inherited from class jakarta.servlet.GenericServlet

    getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • init

      public void init(jakarta.servlet.ServletConfig servletConfig) throws jakarta.servlet.ServletException
      Specified by:
      init in interface jakarta.servlet.Servlet
      Overrides:
      init in class jakarta.servlet.http.HttpServlet
      Throws:
      jakarta.servlet.ServletException
    • setContext

      protected void setContext(RestContext context) throws jakarta.servlet.ServletException
      Sets the context object for this servlet.

      This method is effectively a no-op if init(ServletConfig) has already been called.

      Parameters:
      context - Sets the context object on this servlet.
      Throws:
      jakarta.servlet.ServletException - If error occurred during initialization.
    • getPath

      public String getPath()
      Returns the path for this resource as defined by the @Rest(path) annotation or RestContext.Builder.path(String) method concatenated with those on all parent classes.
      Returns:
      The path defined on this servlet, or an empty string if not specified.
    • service

      public void service(jakarta.servlet.http.HttpServletRequest r1, jakarta.servlet.http.HttpServletResponse r2) throws jakarta.servlet.ServletException, InternalServerError, IOException
      The main service method.

      Subclasses can optionally override this method if they want to tailor the behavior of requests.

      Overrides:
      service in class jakarta.servlet.http.HttpServlet
      Throws:
      jakarta.servlet.ServletException
      InternalServerError
      IOException
    • destroy

      public void destroy()
      Specified by:
      destroy in interface jakarta.servlet.Servlet
      Overrides:
      destroy in class jakarta.servlet.GenericServlet
    • getContext

      Returns the read-only context object that contains all the configuration information about this resource.

      This object is null during the call to init(ServletConfig) but is populated by the time GenericServlet.init() is called.

      Resource classes that don't extend from RestServlet can add the following method to their class to get access to this context object:

      public void init(RestServletContext context) throws Exception;

      Returns:
      The context information on this servlet.
    • log

      public void log(Level level, String msg, Object... args)
      Log a message.

      Subclasses can intercept the handling of these messages by overriding doLog(Level, Throwable, Supplier).

      Parameters:
      level - The log level.
      msg - The message to log.
      args - Optional MessageFormat-style arguments.
    • log

      public void log(Level level, Throwable cause, String msg, Object... args)
      Log a message.

      Subclasses can intercept the handling of these messages by overriding doLog(Level, Throwable, Supplier).

      Parameters:
      level - The log level.
      cause - The cause.
      msg - The message to log.
      args - Optional MessageFormat-style arguments.
    • doLog

      protected void doLog(Level level, Throwable cause, Supplier<String> msg)
      Main logger method.

      The default behavior logs a message to the Java logger of the class name.

      Subclasses can override this method to implement their own logger handling.

      Parameters:
      level - The log level.
      cause - Optional throwable.
      msg - The message to log.
    • getRequest

      Returns the current thread-local HTTP request.
      Returns:
      The current thread-local HTTP request, or null if it wasn't created.
    • getResponse

      Returns the current thread-local HTTP response.
      Returns:
      The current thread-local HTTP response, or null if it wasn't created.