Default Parts
By default, HTTP parts that don't have value (such as missing query parameters) end up with null values:
@RestPost("/example")
public String doGetExample1(
@Query("p1") int p1,
@FormData("f1") MyBean f1,
@Header("Accept-Language") AcceptLanguage h1
) {
if (p1 == null) p1 = -1;
if (f1 == null) f1 = DEFAULT_BEAN;
if (h1 == null) h1 = AcceptLanguage.of("en");
}
You have several options to provide default values for HTTP parts.
The most common is to simply use Optional parameters and handle default values programmatically:
@RestPost("/example")
public String doGetExample1(
@Query("p1") Optional<Integer> p1,
@FormData("f1") Optional<MyBean> f1,
@Header("Accept-Language") Optional<AcceptLanguage> h1
) {
int _p1 = p1.orElse(-1);
MyBean _f1 = f1.orElse(DEFAULT_BEAN);
AcceptLanguage _h1 = h1.orElse(AcceptLanguage.of("en"));
}
You can also specify default values on the annotations:
@RestPost("/example")
public String doGetExample1(
@Query(name="p1", def="-1") int p1,
@FormData(name="f1", def="foo=bar,baz=qux") MyBean f1,
@Header(name="Accept-Language", def="en") AcceptLanguage lang
) {
...
}
A third option is to specify default values via the Rest and RestOp annotations.
Example
// Servlet with default headers
@Rest(
// Assume "text/json" Accept value when Accept not specified
defaultRequestHeaders={"Accept: text/json"},
// Add a version header attribute to all responses
defaultResponseHeaders={"X-Version: 1.0"}
)
public MyRestServlet extends BasicRestServlet {
...
}
Default parts can also be specified programmatically through any of the following methods: