Search code examples
micronaut

Micronaut configuration property default value


I am not sure this is a bug. As a matter of facts this may be the expected behavior.

Assume I have the following configuration class

@ConfigurationProperties("app")
public class ApplicationConfiguration {

    public String uri = "/api/v1";

    // I also tested with optional, no difference
    // public Optional<String> uri = Optional.of("/api/v1");
}

and I use the annotation @Controller("${app.uri}/some/path") on a controller.

If I do not add an entry in application.yml then I end up with the following error:

Could not resolve placeholder ${app.uri} in value: ${app.uri}/some/path

While if I add the following lines to my application.yml

app:
  uri: /api/v2

Then every thing works: the placeholder is resolved to /api/v2 and also the value in the injectable configuration object is set to /api/v2 instead of the default value /api/v1.

I would have expected that if I do not set a value in application.yml the default value in the class would have been resolved (/api/v1).

Again this may be just by design, and it is fine. I am just wondering if I am doing something wrong and, in this case, how to fix.


Solution

  • This is the expected behavior. It's not feasible to know if a property has a default value in a configuration class. To supply the default value in that context, simply do:

    @Controller("${app.uri:/api/v1}/some/path")