6

I'm trying to define a spring data source url like so:

spring:
  datasource:
    url: "jdbc:${vcap.services.compose-for-mysql.credentials.uri}?useSSL=true&requireSSL=true&verifyServerCertificate=true"
    username: ${vcap.services.compose-for-mysql.credentials.username}
    password: ${vcap.services.compose-for-mysql.credentials.password}
    driver-class-name: com.mysql.jdbc.Driver

Where vcap.services.compose-for-mysql.credentials.uri is set to mysql://xxxx:xxxx@xxxx.1.dblayer.com:28018/compose.

I need the url to look like this:

jdbc:mysql://xxxx:xxxx@xxxx.1.dblayer.com:28018/compose?useSSL=true&requireSSL=true&verifyServerCertificate=true

However, Spring doesn't appear to be able to handle this:

Could not get JDBC Connection; nested exception is java.sql.SQLException: Driver:com.mysql.jdbc.Driver@6c6efbc8 returned null for URL:jdbc:${vcap.services.compose-for-mysql.credentials.uri}?useSSL=true&requireSSL=true&verifyServerCertificate=true

Is there a way that I can construct the url using a yaml file, or do I need to use another approach such as xml configuration?


Update

I've tried:

url: ${'jdbc:'}${vcap.services.compose-for-mysql.credentials.uri}{'?useSSL=true&requireSSL=true&verifyServerCertificate=true'}

But get the error:

java.lang.IllegalArgumentException: URL must start with 'jdbc'

Also tried:

url: jdbc:${vcap.services.compose-for-mysql.credentials.uri}?useSSL=true&requireSSL=true&verifyServerCertificate=true

But get the error:

Driver:com.mysql.jdbc.Driver@567443ab returned null for URL:jdbc:${vcap.services.compose-for-mysql.credentials.uri}?useSSL=true&requireSSL=true&verifyServerCertificate=true

Chris Snow
  • 23,813
  • 35
  • 144
  • 309
  • Is the `vcap.services...` values inside the same property file? – kagmole Jun 15 '17 at 12:17
  • No, spring boot creates this value from an environment variable – Chris Snow Jun 15 '17 at 12:25
  • Try `${env:vcap.service...}` – kagmole Jun 15 '17 at 12:32
  • results in `Driver:com.mysql.jdbc.Driver@54a388b returned null for URL:jdbc:vcap.services.compose-for-mysql.credentials.uri?useSSL=true&requireSSL=true&verifyServerCertificate=true` – Chris Snow Jun 15 '17 at 12:42
  • Maybe `${sys:vcap.service...}` but that would not make sense since it is for system properties and not environment variables... – kagmole Jun 15 '17 at 16:43
  • same issue here any news? – MiniScalope Oct 06 '21 at 11:19
  • What version of Spring Boot are you using? I just tested this with 2.5.5 and it works for me. I can set `url: "jdbc:${url}?useSSL=true&requireSSL=true&verifyServerCertificate=true"` in aplication.yml and run my app with `URL=db.example.com ./mvnw spring-boot:run` and it'll print `jdbc:db.example.com?useSSL=true&requireSSL=true&verifyServerCertificate=true`. Similar post where it's also working: https://stackoverflow.com/a/54911958/1585136 – Daniel Mikusa Oct 28 '21 at 15:09

0 Answers0