Annotation Interface RestInit


Identifies a method that gets called during servlet initialization.

This method is called from within the Servlet.init(ServletConfig) method after the resource's annotation-driven configuration has been applied to the in-flight RestContext, but before any HTTP request is dispatched. It's the supported hook for one-time setup work such as loading data files or precomputing caches.

Method parameters are resolved from the bean store the same way as any other Juneau-injected method. ServletConfig, ServletContext, the resource instance itself, and any bean registered via @Bean or the configured bean-store hooks are all resolvable. Zero-argument variants are also supported.

Note (10.0): two related Builder-injection protocols have been removed in this release. They had zero real-world callers across the codebase before deletion:

  • Per-operation: @RestInit public void init(RestOpContext.Builder b) (invoked once per @RestOp-annotated method) — replaced by declarative @RestOp(...) attributes, @Bean(name=, methodScope=)-named beans, or class-level @RestInit hooks.
  • Class-level Builder injection: @RestInit public void init(RestContext.Builder b) (which injected the in-flight resource-level builder so the hook could imperatively mutate it) — replaced by the same declarative surfaces. Migrate by moving each builder.xxx(...) call to the equivalent @Rest(xxx=...) annotation attribute or @Bean-named bean.

The remaining supported @RestInit hook shape is one whose parameters are bean-store-resolvable (no RestContext.Builder or RestOpContext.Builder). The example below uses the resource instance itself, but @Bean-supplied beans, ServletConfig, etc. work the same way.

Example:

@Rest(...) public class PetStoreResource extends BasicRestServlet implements BasicUniversalJenaConfig { // Our database. private Map<Integer,Pet> petDB; @RestInit public void onInit() throws Exception { // Load our database from a local JSON file. petDB = JsonParser.DEFAULT.read(getClass().getResourceAsStream("PetStore.json"), LinkedHashMap.class, Integer.class, Pet.class); } }

Notes:
  • The method should return void although if it does return any value, the value will be ignored.
  • The method should be public although other visibilities are valid if the security manager allows it.
  • Static methods can be used.
  • Multiple init methods can be defined on a class.
    Init methods on parent classes are invoked before init methods on child classes.
    The order of init method invocations within a class is alphabetical, then by parameter count, then by parameter types.
  • The method can throw any exception causing initialization of the servlet to fail.
  • Note that if you override a parent method, you probably need to call super.parentMethod(...).
    The method is still considered part of the parent class for ordering purposes even though it's overridden by the child class.
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Optional description for the exposed API.
  • Element Details

    • description

      Optional description for the exposed API.
      Returns:
      The annotation value.
      Since:
      9.2.0
      Default:
      {}