Property Type Conversion

Micronaut uses the ConversionService bean to convert values when resolving properties. You can register additional converters for types not supported by Micronaut by defining beans that implement the TypeConverter interface.

Micronaut features some built-in conversions that are useful, which are detailed below.

Duration Conversion

Durations can be specified by appending the unit with a number. Supported units are s, ms, m etc. The following table summarizes examples:

Table 1. Duration Conversion
Configuration ValueResulting Value

10ms

Duration of 10 milliseconds

10m

Duration of 10 minutes

10s

Duration of 10 seconds

10d

Duration of 10 days

10h

Duration of 10 hours

10ns

Duration of 10 nanoseconds

PT15M

Duration of 15 minutes using ISO-8601 format

For example to configure the default HTTP client read timeout:

Using Duration Values

  1. micronaut:
  2. http:
  3. client:
  4. read-timeout: 15s

List / Array Conversion

Lists and arrays can be specified in Java properties files as comma-separated values, or in YAML using native YAML lists. The generic types are used to convert the values. For example in YAML:

Specifying lists or arrays in YAML

  1. my:
  2. app:
  3. integers:
  4. - 1
  5. - 2
  6. urls:
  7. - http://foo.com
  8. - http://bar.com

Or in Java properties file format:

Specifying lists or arrays in Java properties comma-separated

  1. my.app.integers=1,2
  2. my.app.urls=http://foo.com,http://bar.com

Alternatively you can use an index:

Specifying lists or arrays in Java properties using index

  1. my.app.integers[0]=1
  2. my.app.integers[1]=2

For the above example configurations you can define properties to bind to with the target type supplied via generics:

  1. List<Integer> integers;
  2. List<URL> urls;

Readable Bytes

You can annotate any setter parameter with @ReadableBytes to allow the value to be set using a shorthand syntax for specifying bytes, kilobytes etc. For example the following is taken from HttpClientConfiguration:

Using @ReadableBytes

  1. public void setMaxContentLength(@ReadableBytes int maxContentLength) {
  2. this.maxContentLength = maxContentLength;
  3. }

With the above in place you can set micronaut.http.client.max-content-length using the following values:

Table 2. @ReadableBytes Conversion
Configuration ValueResulting Value

10mb

10 megabytes

10kb

10 kilobytes

10gb

10 gigabytes

1024

A raw byte length

Formatting Dates

The @Format annotation can be used on setters to specify the date format to use when binding java.time date objects.

Using @Format for Dates

  1. public void setMyDate(@Format("yyyy-MM-dd") LocalDate date) {
  2. this.myDate = date;
  3. }