{title:'Default Parts', created:'9.0.0'}

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 {@link java.util.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); | Bean _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 {@link oajr.annotation.Rest} and {@link oajr.annotation.RestOp} annotations.

- {@link oajr.annotation.Rest}
  - {@link oajr.annotation.Rest#defaultAccept() defaultAccept}
  - {@link oajr.annotation.Rest#defaultCharset() defaultCharset}
  - {@link oajr.annotation.Rest#defaultContentType() defaultContentType}
  - {@link oajr.annotation.Rest#defaultRequestAttributes() defaultRequestAttributes}
  - {@link oajr.annotation.Rest#defaultRequestHeaders() defaultRequestHeaders}
  - {@link oajr.annotation.Rest#defaultResponseHeaders() defaultResponseHeaders}
- {@link oajr.annotation.RestOp}
  - {@link oajr.annotation.RestOp#defaultAccept() defaultAccept}
  - {@link oajr.annotation.RestOp#defaultCharset() defaultCharset}
  - {@link oajr.annotation.RestOp#defaultContentType() defaultContentType}
  - {@link oajr.annotation.RestOp#defaultRequestAttributes() defaultRequestAttributes}
  - {@link oajr.annotation.RestOp#defaultRequestFormData() defaultRequestFormData}
  - {@link oajr.annotation.RestOp#defaultRequestHeaders() defaultRequestHeaders}
  - {@link oajr.annotation.RestOp#defaultRequestQueryData() defaultRequestQueryData}
  - {@link oajr.annotation.RestOp#defaultResponseHeaders() defaultResponseHeaders}

##### 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:

- {@link oajr.RestContext.Builder}
  - {@link oajr.RestContext.Builder#defaultAccept(String) defaultAccept(String)}
  - {@link oajr.RestContext.Builder#defaultCharset(Charset) defaultCharset(Charset)}
  - {@link oajr.RestContext.Builder#defaultContentType(String) defaultContentType(String)}
  - {@link oajr.RestContext.Builder#defaultRequestAttributes() defaultRequestAttributes()}
  - {@link oajr.RestContext.Builder#defaultRequestAttributes(NamedAttribute...) defaultRequestAttributes(NamedAttribute...)}
  - {@link oajr.RestContext.Builder#defaultRequestHeaders() defaultRequestHeaders()}
  - {@link oajr.RestContext.Builder#defaultRequestHeaders(Header...) defaultRequestHeaders(Header...)}
  - {@link oajr.RestContext.Builder#defaultResponseHeaders() defaultResponseHeaders()}
  - {@link oajr.RestContext.Builder#defaultResponseHeaders(Header...) defaultResponseHeaders(Header...)}
- {@link oajr.RestOpContext.Builder}
  - {@link oajr.RestOpContext.Builder#defaultRequestAttributes() defaultRequestAttributes()}
  - {@link oajr.RestOpContext.Builder#defaultRequestAttributes(NamedAttribute...) defaultRequestAttributes(NamedAttribute...)}
  - {@link oajr.RestOpContext.Builder#defaultRequestFormData() defaultRequestFormData()}
  - {@link oajr.RestOpContext.Builder#defaultRequestFormData(NameValuePair...) defaultRequestFormData(NameValuePair...)}
  - {@link oajr.RestOpContext.Builder#defaultRequestHeaders() defaultRequestHeaders()}
  - {@link oajr.RestOpContext.Builder#defaultRequestHeaders(Header...) defaultRequestHeaders(Header...)}
  - {@link oajr.RestOpContext.Builder#defaultRequestQueryData() defaultRequestQueryData()}
  - {@link oajr.RestOpContext.Builder#defaultRequestQueryData(NameValuePair...) defaultRequestQueryData(NameValuePair...)}
  - {@link oajr.RestOpContext.Builder#defaultResponseHeaders() defaultResponseHeaders()}
  - {@link oajr.RestOpContext.Builder#defaultResponseHeaders(Header...) defaultResponseHeaders(Header...)}