Properties
As shown in previous sections, Juneau serializers and parsers are highly-configurable through properties.
(See {@doc ConfigurableProperties})
These properties can be defined for serializers and parsers registered on a REST resource via the following:
- {@link oajr.annotation.RestResource#properties() RestResource(properties)}
- {@link oajr.RestContextBuilder} - Various methods on the context builder.
import static org.apache.juneau.BeanContext.*;
import static org.apache.juneau.serializer.Serializer.*;
import static org.apache.juneau.json.JsonSerializer.*;
// Servlet with properties applied
@RestResource(
properties={
// Bean properties should be sorted alphabetically.
@Property(name=BEAN_sortProperties, value="true"),
// Nulls should not be serialized
@Property(name=SERIALIZER_trimNulls, value="true"),
// Solidus characters should be escaped in JSON
@Property(name=JSON_escapeSolidus, value="true")
}
)
public MyRestServlet extends BasicRestServlet {...}
The programmatic equivalent to this is:
// Servlet with properties applied
@RestResource(...)
public MyRestServlet extends BasicRestServlet {
public MyRestServlet(RestContextBuilder builder) {
builder
.sortProperties(); // Note: RestContextBuilder extends from BeanContextBuilder
.set(SERIALIZER_trimNulls, true);
.set(JSON_escapeSolidus, true);
}
}
Properties can also be overridden at the Java method level:
- {@link oajr.annotation.RestMethod#properties() RestMethod(properties)}
- {@link oajr.RequestProperties}
// GET method with method-level properties
@RestMethod(
name=GET, path="/*",
properties={
// Bean properties should be sorted alphabetically.
@Property(name=BEAN_sortProperties, value="true"),
// Nulls should not be serialized
@Property(name=SERIALIZER_trimNulls, value="true"),
// Solidus characters should be escaped in JSON
@Property(name=JSON_escapeSolidus, value="true")
}
public Object doGet() {
...
}
Using the {@link oajr.RequestProperties} object:
// Access it from RestRequest.
public Object doGet(RestRequest req) {
RequestProperties properties = req.getProperties();
properties.put(JSON_escapeSolidus, true);
}
// Or just pass in a RequestProperties object.
public Object doGet(RequestProperties properties) {
properties.put(JSON_escapeSolidus, true);
}
Properties set via {@link oajr.RequestProperties} are session-override
properties that are passed in through {@link oaj.serializer.SerializerSessionArgs}
and {@link oaj.parser.ParserSessionArgs} and can only be used on configuration settings
marked as Session property: true
.
Properties are open-ended and can be used for other purposes.
They're made available through the following methods:
- {@link oajr.RestContext#getProperties()}
- {@link oajr.RestRequest#getProperties()}
See Also:
- {@link oajr.annotation.RestResource#flags() RestResource(flags)} - Shorthand for boolean properties.
- {@link oajr.annotation.RestMethod#flags() RestMethod(flags)} - Shorthand for boolean properties.
- {@link oajr.RestContextProperties}
- {@link oajr.RestMethodProperties}
- {@link oajr.RequestProperties}