Annotation Type RestInject


Rest bean injection annotation.

Used on methods of Rest-annotated classes to denote methods and fields that override and customize beans used by the REST framework.

Example

// Rest resource that uses a customized call logger. @Rest public class MyRest extends BasicRestServlet { // Option #1: As a field. @RestInject CallLogger myCallLogger = CallLogger.create().logger("mylogger").build(); // Option #2: As a method. @RestInject public CallLogger myCallLogger() { return CallLogger.create().logger("mylogger").build(); } }

The name()/value() attributes are used to differentiate between named beans.

Example

// Customized default request headers. @RestInject("defaultRequestHeaders") HeaderList defaultRequestHeaders = HeaderList.create().set(ContentType.TEXT_PLAIN).build(); // Customized default response headers. @RestInject("defaultResponseHeaders") HeaderList defaultResponseHeaders = HeaderList.create().set(ContentType.TEXT_PLAIN).build();

The methodScope() attribute is used to define beans in the scope of specific RestOp-annotated methods.

Example

// Set a default header on a specific REST method. // Input parameter is the default header list builder with all annotations applied. @RestInject(name="defaultRequestHeaders", methodScope="myRestMethod") public HeaderList.Builder myRequestHeaders(HeaderList.Builder builder) { return builder.set(ContentType.TEXT_PLAIN); } // Method that picks up default header defined above. @RestGet public Object myRestMethod(ContentType contentType) { ... }

This annotation can also be used to inject arbitrary beans into the bean store which allows them to be passed as resolved parameters on RestOp-annotated methods.

Example

// Custom beans injected into the bean store. @RestInject MyBean myBean1 = new MyBean(); @RestInject("myBean2") MyBean myBean2 = new MyBean(); // Method that uses injected beans. @RestGet public Object doGet(MyBean myBean1, @Named("myBean2") MyBean myBean2) { ... }

This annotation can also be used on uninitialized fields. When fields are uninitialized, they will be set during initialization based on beans found in the bean store.

Example

// Fields that get set during initialization based on beans found in the bean store. @RestInject CallLogger callLogger; @RestInject BeanStore beanStore; // Note that BeanStore itself can be accessed this way.

Notes:
  • Methods and fields can be static or non-static.
  • Any injectable beans (including spring beans) can be passed as arguments into methods.
  • Bean names are required when multiple beans of the same type exist in the bean store.
  • By default, the injected bean scope is class-level (applies to the entire class). The methodScope() annotation can be used to apply to method-level only (when applicable).

Any of the following types can be customized via injection:

Bean classBean qualifying namesScope
BeanContext
BeanContext.Builder
class
method
BeanStore
BeanStore.Builder
class
CallLogger
CallLogger.Builder
class
Configclass
DebugEnablement
DebugEnablement.Builder
class
EncoderSet
EncoderSet.Builder
class
method
FileFinder
FileFinder.Builder
class
HeaderList
HeaderList
"defaultRequestHeaders"
"defaultResponseHeaders"
class
method
HttpPartParser
HttpPartParser.Creator
class
method
HttpPartSerializer
HttpPartSerializer.Creator
class
method
JsonSchemaGenerator
JsonSchemaGenerator.Builder
class
method
Loggerclass
Messages
Messages.Builder
class
MethodExecStore
MethodExecStore.Builder
class
MethodList"destroyMethods"
"endCallMethods"
"postCallMethods"
"postInitChildFirstMethods"
"postInitMethods"
"preCallMethods"
"startCallMethods"
class
NamedAttributeMap
NamedAttributeMap
"defaultRequestAttributes"class
method
ParserSet
ParserSet.Builder
class
method
PartList
PartList
"defaultRequestQueryData"
"defaultRequestFormData"
method
ResponseProcessorList
ResponseProcessorList.Builder
class
RestChildren
RestChildren.Builder
class
RestConverterList
RestConverterList.Builder
method
RestGuardList
RestGuardList.Builder
method
RestMatcherList
RestMatcherList.Builder
method
RestOpArgList
RestOpArgList.Builder
class
RestOperations
RestOperations.Builder
class
SerializerSet
SerializerSet.Builder
class
method
StaticFiles
StaticFiles.Builder
class
SwaggerProvider
SwaggerProvider.Builder
class
ThrownStore
ThrownStore.Builder
class
UrlPathMatcherListmethod
VarListclass
VarResolver
VarResolver.Builder
class
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    The short names of the methods that this annotation applies to.
    The bean name to use to distinguish beans of the same type for different purposes.
    Same as name().
  • Element Details

    • name

      The bean name to use to distinguish beans of the same type for different purposes.

      For example, there are two HeaderList beans: "defaultRequestHeaders" and "defaultResponseHeaders". This annotation would be used to differentiate between them.

      Returns:
      The bean name to use to distinguish beans of the same type for different purposes, or blank if bean type is unique.
      Default:
      ""
    • value

      Same as name().
      Returns:
      The bean name to use to distinguish beans of the same type for different purposes, or blank if bean type is unique.
      Default:
      ""
    • methodScope

      The short names of the methods that this annotation applies to.

      Can use "*" to apply to all methods.

      Ignored for class-level scope.

      Returns:
      The short names of the methods that this annotation applies to, or empty if class-scope.
      Default:
      {}