{title:'Lifecycle Hooks', updated:'9.0.0'}

Lifecycle hooks allow you to hook into lifecycle events of the servlet/resource creation and REST calls.

For example, if you want to add an initialization method to your resource:

| @Rest(...) | public class MyResource extends BasicRestObject { | | // Our database. | private Map<Integer,Object> myDatabase; | | @RestInit | public void initMyDatabase(RestContext.Builder builder) throws Exception { | myDatabase = new LinkedHashMap<>(); | } | }

Or if you want to intercept REST calls:

| @Rest(...) | public class MyResource extends BasicRestObject { | | // Add a request attribute to all incoming requests. | @RestPreCall | public void onPreCall(RestRequest req) { | req.setAttribute("foo", "bar"); | } | }

The following lifecycle annotations are provided.

- Resource lifecycle events:
  - {@link oajr.annotation.RestInit} - Right before initialization.
  - {@link oajr.annotation.RestPostInit} - Right after initialization.
  - {@link oajr.annotation.RestDestroy} - Right before servlet destroy.
- REST call lifecycle events:
  - {@link oajr.annotation.RestStartCall} - At the beginning of a REST call.
  - {@link oajr.annotation.RestPreCall} - Right before the @RestOp method is invoked.
  - {@link oajr.annotation.RestPostCall} - Right after the @RestOp method is invoked.
  - {@link oajr.annotation.RestEndCall} - At the end of the REST call after the response has been flushed.