Bindable Types

Generally any type that can be converted from a String representation to a Java type via the ConversionService API can be bound to.

This includes most common Java types, however additional TypeConverter instances can be registered simply be creating @Singleton beans of type TypeConverter.

The handling of nullability deserves special mention. Consider for example the following example:

  1. @Get("/headerInferred")
  2. public String headerInferred(@Header String contentType) {
  3. // ...
  4. }
  1. @Get("/headerInferred")
  2. String headerInferred(@Header String contentType) {
  3. // ...
  4. }
  1. @Get("/headerInferred")
  2. fun headerInferred(@Header contentType: String): String {
  3. // ...
  4. }

In this case if the HTTP header Content-Type is not present in the request the route is considered invalid, since it cannot be satisfied and a HTTP 400 BAD REQUEST is returned.

If you wish for the Content-Type header to be optional, you can instead write:

  1. @Get("/headerNullable")
  2. public String headerNullable(@Nullable @Header String contentType) {
  3. // ...
  4. }
  1. @Get("/headerNullable")
  2. String headerNullable(@Nullable @Header String contentType) {
  3. // ...
  4. }
  1. @Get("/headerNullable")
  2. fun headerNullable(@Header contentType: String?): String? {
  3. // ...
  4. }

An null string will be passed if the header is absent from the request.

java.util.Optional can also be used, however that is discouraged for method parameters.

Additionally, any DateTime that conforms to RFC-1123 can be bound to a parameter, alternatively the format can be customized with the Format annotation:

  1. @Get("/date")
  2. public String date(@Header ZonedDateTime date) {
  3. // ...
  4. }
  5. @Get("/dateFormat")
  6. public String dateFormat(@Format("dd/MM/yyyy hh:mm:ss a z") @Header ZonedDateTime date) {
  7. // ...
  8. }
  1. @Get("/date")
  2. String date(@Header ZonedDateTime date) {
  3. // ...
  4. }
  5. @Get("/dateFormat")
  6. String dateFormat(@Format("dd/MM/yyyy hh:mm:ss a z") @Header ZonedDateTime date) {
  7. // ...
  8. }
  1. @Get("/date")
  2. fun date(@Header date: ZonedDateTime): String {
  3. // ...
  4. }
  5. @Get("/dateFormat")
  6. fun dateFormat(@Format("dd/MM/yyyy hh:mm:ss a z") @Header date: ZonedDateTime): String {
  7. // ...
  8. }