RestContext
The RestContext object is the workhorse class for all of the configuration of a single REST resource class. It's by-far the most important class in the REST API. Every class annotated with @Rest ends up with an instance of this object.
The object itself is read-only and unchangeable and is initialized with all of the various annotations pulled from the class and methods. All functionality available through annotations have programmatic equivalents through the builder of this class.
The RestContext.Builder class extends BeanContext.Builder allowing you to programmatically set any properties defined on that builder class. It also implements ServletConfig.
To access this object, simply pass it in as a constructor argument or in an INIT hook:
// Option #1 - Pass in through constructor.
public MyResource(RestContext.Builder builder) {
builder
.beanContext(x -> x.swaps(TemporalCalendarSwap.Rfc1123DateTime.class))
.debugEnablement(CONDITIONAL);
}
// Option #2 - Use an init hook.
@RestInit
public void init(RestContext.Builder builder) throws Exception {
builder
.beanContext(x -> x.swaps(TemporalCalendarSwap.Rfc1123DateTime.class))
.debugEnablement(CONDITIONAL);
}
This class is vast. Combined with RestOpContext (which is the equivalent per-method context), these classes define the entire configuration and workflow of the REST API.
There are multiple ways to programmatically alter how RestContext
behaves.
The most straightforward are the following builder methods which are direct equivalents to values defined on the @Rest annotation:
For more complex configurations, access to sub-builders is provided via the following methods:
The builders or built objects above can also be defined as injected beans defined in a Spring Configuration if you wish to do all your app configuration Spring-style. This is described in detail in the juneau-rest-server-springboot documentation.
The programmatic equivalent to the annotated lifecycle methods are below:
It is also possible to override methods on the RestContext class itself by providing your own specialized subclass via the RestContext.Builder.type(Class) method.