{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 { | | // Our database. | private Map<Integer,Object> myDatabase; | | @RestHook(INIT) | public void initMyDatabase(RestContext.Builder builder) throws Exception { | myDatabase = new LinkedHashMap<>(); | } | }

Or if you want to intercept REST calls:

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

The hook events can be broken down into two categories: