Property Type Conversion
When resolving properties Micronaut will use the ConversionService bean to convert 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:
Configuration Value | Resulting Value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
| Duration of 15 minutes using ISO-8601 format |
For example to configure the default HTTP client read timeout:
Using Duration Values
micronaut:
http:
client:
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
my:
app:
integers:
- 1
- 2
urls:
- http://foo.com
- http://bar.com
Or in Java properties file format:
Specifying lists or arrays in Java properties comma-separated
my.app.integers=1,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
my.app.integers[0]=1
my.app.integers[1]=2
For the above example configurations you can define properties to bind to with the target type supplied via generics:
List<Integer> integers;
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
public void setMaxContentLength(@ReadableBytes int maxContentLength) {
this.maxContentLength = maxContentLength;
}
With the above in place you can set micronaut.http.client.max-content-length
using the following values:
Configuration Value | Resulting Value |
---|---|
| 10 megabytes |
| 10 kilobytes |
| 10 gigabytes |
| A raw byte length |
Formatting Dates
The @Format annotation can be used on any setter to allow the date format to be specified when binding java.time
date objects.
Using @Format
for Dates
public void setMyDate(@Format("yyyy-MM-dd") LocalDate date) {
this.myDate = date;
}