Class RestMixin

java.lang.Object
org.apache.juneau.rest.server.servlet.RestMixin
Direct Known Subclasses:
DumpsMixin, ErrorMixin, FaviconMixin, HealthMixin, HtdocMixin, InfoMixin, LoggersMixin, MetricsMixin, NavigationMixin, SeoMixin, StatsMixin, VersionMixin, WellKnownMixin

public abstract class RestMixin extends Object
Optional base class for @Rest mixin classes — the third sibling of RestServlet (servlet flavor) and RestResource (child-resource flavor), completing the servlet / resource / mixin naming triad.

A mixin is a plain @Rest-annotated POJO whose @RestOp methods are pulled into a host servlet/resource context when the host declares @Rest(mixins=ThatMixin.class). Mixins compose through the existing sub-context model; RestMixin does not change that composition mechanism.

Opt-in, not mandatory:

Extending RestMixin is optional. A bare @Rest POJO with no base class remains a fully-supported mixin (annotation-only configuration), exactly as before — the framework reads such mixins reflectively. Capability mixins that ship without state (e.g. the api-docs pack) stay base-less; the new single-responsibility op-mixins extend this base to make the triad membership explicit.

Builder support:

The fluent programmatic-builder surface (RestBuilder/ RestMixin.Builder) lets a mixin be configured programmatically rather than (or in addition to) by annotation — builder-supplied values take precedence over @Rest annotation values. Use builder(Class) for the common case, or subclass RestMixin.Builder for capability mixins that add their own setters.

Reaching the host resource:

Because a mixin's @RestOp methods are bound to a per-mixin RestContext sub-context (the sub-context has no children of its own), host-level introspection performed against the mixin's own context comes back empty. getHostContext() bridges that gap: it returns the RestContext of the host (mixed-into) resource so a mixin op can enumerate the host's child resources, swagger, stats, etc.

// Render the host's child-resource navigation list from a mixin op. @RestGet(path="/") public ChildResourceDescriptions getChildren(RestRequest req) { return new ChildResourceDescriptions(getHostContext(), req); }

See Also:
Since:
10.0.0
  • Constructor Details

    • RestMixin

      protected RestMixin()
      Default constructor.
    • RestMixin

      protected RestMixin(RestBuilder<?> builder)
      Builder-injection constructor.
      Parameters:
      builder - The programmatic configuration builder. May be null.
  • Method Details

    • getRestBuilder

      Returns the programmatic configuration builder stashed on this mixin, or null if none.
      Returns:
      The stashed builder, or null if none was injected.
    • builder

      public static <R extends RestMixin> RestMixin.DefaultBuilder<R> builder(Class<R> type)
      Creates a new fluent builder for programmatically configuring an instance of the specified mixin type.
      Type Parameters:
      R - The mixin type.
      Parameters:
      type - The mixin type to build. Must not be null.
      Returns:
      A new builder.
    • setContext

      protected void setContext(RestContext value)
      Captures the per-mixin RestContext sub-context this mixin instance is bound to.

      Invoked reflectively by the host's RestContext.buildMixinContext(...) while composing the mixin (mirroring the setContext(RestContext) contract honored by RestServlet and RestResource for child resources). The supplied context is the mixin sub-context whose parent is the host — that linkage is what getHostContext() reads.

      Parameters:
      value - The mixin sub-context. Must not be null.
    • getHostContext

      Returns the RestContext of the host (mixed-into) resource this mixin is composed into.

      A mixin's @RestOp methods are bound to a per-mixin sub-context that has no children of its own; this accessor returns the host context so a mixin op (or config-time code) can introspect the host — most commonly its child resources via RestContext.getRestChildren() for the navigation page. It is backed by the mixin sub-context's already-populated parent linkage, so it is usable at config time and does not depend on an in-flight request.

      Edge cases:
      • Standalone / no host — when a RestMixin subclass is instantiated directly rather than composed via @Rest(mixins=...), this returns null, mirroring RestContext.getParentContext()'s top-level contract. Callers needing host-only behavior should null-check.
      • Nested mixins — under nested @Rest(mixins=...) the flat-inheritance rule collects every mixin as a mixin of the single top-level host, so this returns that top-level host (never an intermediate mixin).
      Returns:
      The host resource's RestContext, or null when this mixin is not composed into a host.
      Since:
      10.0.0