micronaut yaml configuration: special character in default value

475 Views Asked by At

This is what I'm having problems with in my application.yml file:

url: ${DATASOURCE_URL:jdbc:postgresql://localhost:5432/postgres}

It seems to treat it like it should cut it at the last colon:

Message: Invalid JDBC URL [5432/postgres]. JDBC URLs must start with 'jdbc'.

Tried escaping with ', " and \ in different sections, nothing seems to work. Tried even combining them

url: "${DATASOURCE_URL:jdbc\\:postgresql\\://localhost\\:5432/postgres}"

and

url: "${DATASOURCE_URL:jdbc\:postgresql\://localhost\:5432/postgres}"

Edit: this value needs to be empty if not defined due to Micronaut Test Resources I'm using. It will create test resources only if value is empty or not defined.

Edit: realized I'm using micronaut instead of spring boot. No wonder the suggestions didn't work.

Edit: I can't omit the url configuration, as I have two different datasources. Omitting would only work for the default one AFAIK - I have to configure the additional datasource somehow, which is what I'm trying to achieve here.

3

There are 3 best solutions below

0
ShingJo On BEST ANSWER

For Micronaut, your url configuration with a placeholder "DATASOURCE_URL" and a default value should look like:

url: ${DATASOURCE_URL:`jdbc:postgresql://localhost:5432/postgres`}

Uses backticks "`" to escape the JDBC URL because of the colon ":".

If you are using Micronaut Test Resources, you can omit the URL configuration all together.

datasources:
  default:
    driver-class-name: org.postgresql.Driver # Used by Micronaut Data
    dialect: POSTGRES 
    #schema-generate: CREATE_DROP
    db-type: postgres # Used by Micronaut Test Resources     
1
elligar34 On

Here you try to declare a spring variable by assigning it a default value. So you have to give your variable the name of the path of your application.yml attribute.

For instance, I see your attribute is named "url" : If your application.yml looks like that

yourapp:
  datasource:
    url: ${yourapp.datasource.url:default_value}

Your var in the ${} should be named : ${yourapp.datasource.url:default_value}

2
BoboTheKnight On

It might need to declare 'host' and 'port' separately.

Try this: url: jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5432}/postgres