h2 localhost url different from testdb - spring-boot

I am using below spring boot config:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
When my spring app comes up, I see the following:
H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:304a69fe-27f6-4271-a5c3-015f06910885'
However if i set the below in property file, i do see testdb being connected:
spring.datasource.url=jdbc:h2:mem:testdb
Can someone please let me know why do i need to explicitly set the url in property file? I had created another spring boot app recently with the exact same config but with spring boot version 2.2.4.RELEASE where h2 connected by default to testdb without setting it in property file.
Thanks!

Reconsider default for spring.datasource.generate-unique-name as the current one makes test cases brittle #16747
commit
This change ensures that each test in a test suite that shares an
application context gets a unique embedded database, to prevent
inconsistent embedded database state between tests.
You can revert to the previous behavior by following setting:
spring.datasource.generate-unique-name=false

Found out that with the latest versions of Spring Boot (2.3+), the H2 database name is randomly generated each time you restart the server. Similar post: springboot 2.3.0 while connecting to h2 database

Update:
As you are using h2 console, you probably have a property called
spring.h2.console.enabled=true
If so then Spring's H2ConsoleAutoConfiguration class gets enabled and it does the auto-configuration as given below. (Check here )
If you are using any of this annotations - #DataJdbcTest, #DataJpaTest and #JdbcTest in your test, then Spring through #AutoConfigureTestDatabase will call TestDatabaseAutoConfiguration, which in turn, by default, will configure an in-memory embedded database instance with an auto generated unique name.
If you want to solve the problem for single test case, please use:
#AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
If you want this to apply for all test cases, then please have this property in application.yaml
spring:
test:
database:
replace: none

Related

Azure AppConfiguration with Spring Boot 2.5.x

Question
How to integrate an Azure AppConfiguration with SpringBoot 2.5.x or higher?
Info
Im trying to use an Azure AppConfiguration resource with a Spring Boot 2.5.4 project. Unfortunately I cant get it to read a setting from the AppConfiguration or even connect to it as far as I can tell.
The project is newly created with the Spring Initializr where I only added
Spring Boot Starter Web
Spring Boot Starter Security
Spring Boot Starter WebSocket
Afterwards I tried following the Microsoft Quickstart documentation with no success. The documentation mentions that its using Spring 2.4.x so I assume some changes broke it.
I also tried to identify the issue by looking through some Azure Spring Boot Code Samples.
All the examples so far use the bootstrap.properties file which I learned during my search so far is deprecated. Moving the settings to the application.yml or enabling use-legacy-processing: true did not work either. Any ideas?
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-cloud-appconfiguration-config</artifactId>
<version>2.0.0</version>
</dependency>
...
application.yml
spring:
config:
use-legacy-processing: true
profiles:
active: "develop"
application:
name: "MySampleService"
cloud:
azure:
appconfiguration:
stores:
- connection-string: "SomeAzureAppConfigurationResourceConnectionString"
label: ${spring.profiles.active}
#mysampleservice:
# message: "this is a message from file"
AppConfiguration Resource
Im not entirely sure about the format for the setting name. I tried to build the format based on this documentation.
The configuration classes should be fine since commenting in the mysampleservice causes the value of message beeing used.
Any hints are appreciated!
Some more info to elaborate on the accepted answer
The documentation linked in the answer refers to two different packages. The one linked right at the start in the maven repository is spring-cloud-azure-appconfiguration-config while the one used further down is azure-spring-cloud-appconfiguration-config. The second one works with the bootstrap.properties file.
Working pom.xml and bootstrap.properties:
...
<dependencies>
<!-- Dependency to load configuration from azure app configuration resource. Note that additional settings are required in bootstrap.properties
Documentation of settings: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/appconfiguration/azure-spring-cloud-starter-appconfiguration-config
-->
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-cloud-appconfiguration-config-web</artifactId>
<version>2.1.0</version>
</dependency>
...
# Use this to enable or disable the cloud config, disabling it results in application.yaml beeing used.
spring.cloud.azure.appconfiguration.enabled=true
# Connection string to azure app configuration resource
spring.cloud.azure.appconfiguration.stores[0].connection-string= Endpoint=https://myofficeconfiguration.azconfig.io;Id=zUcT-l9-s0:PFYfW7WM0/Pz7WZOnH3v;Secret=JTB9myJqGekDAJ5m8Z1vjmkJZrPd88JbOEE3EqoqJYs=
# Configured filters for settings in the previous defined app configuration resource
spring.cloud.azure.appconfiguration.stores[0].selects[0].key-filter = /mysampleservice/
spring.cloud.azure.appconfiguration.stores[0].selects[0].label-filter = Sample
spring.cloud.azure.appconfiguration.stores[0].selects[1].key-filter = /notificationservice/
spring.cloud.azure.appconfiguration.stores[0].selects[1].label-filter = Sample2
bootstrap.yml/bootstrap.properties can still be used, they are no longer part of the base Spring packages.
Also, you want to use this doc for 2.0.0 and newer https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/appconfiguration/azure-spring-cloud-starter-appconfiguration-config.

Springboot 2.0.2 not working with thymeleaf template, throwing 404 error

I am using Springboot 1.5.7 for my rest api application, and I am using thymeleaf template to send emails from my api. But when I updated the version of spring boot to 2.0.2 its throwing 404 error i.e "Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers".
Below is the config I have in application.yml
spring:
thymeleaf:
cache: false
enabled: true
mode: HTML5
prefix: /templates/
suffix: .html
thymeleaf version in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
Below is the template structure i am using,
My app release is very near and I am badly stuck with this problem, if someone can provide me workaround then it would be a great help.
Remove prefix: /templates/ from application.yml
If still does not work, add thymeleaf-layout-dialect dependency (See: Thymeleaf stopped resolving layout templates after migrating to Thymeleaf 3)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
FYI, I put my views in WEB-INF/webapp/views, so I am using prefix: /WEB-INF/webapp/views/ (spring boot war deploy to tomcat)
As stated in the 2.0 migration guide.
The Thymeleaf starter included the thymeleaf-layout-dialect dependency
previously. Since Thymeleaf 3.0 now offers a native way to implement
layouts,
we removed that mandatory dependency and leave this choice up to you.
If your application is relying on the layout-dialect project, please
add it explicitly as a dependency.
Adding the following dependencies should work
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
SOURCE : https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#template-engines

ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

I have written a spring batch application using Spring boot. When I am trying to run that application using command line and classpath on my local system it is running fine. However, when I tried to run it on linux server it is giving me following exception
Unable to start web server; nested exception is
org.springframework.context.ApplicationContextException:
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
Below is the way I am running it:
java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log
Case 1:
#SpringBootApplication annotation missing in your spring boot starter class.
Case 2:
For non-web applications, disable web application type in the properties file.
In application.properties:
spring.main.web-application-type=none
If you use application.yml then add:
spring:
main:
web-application-type: none
For web applications, extends *SpringBootServletInitializer* in the main class.
#SpringBootApplication
public class YourAppliationName extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(YourAppliationName.class, args);
}
}
Case 3:
If you use spring-boot-starter-webflux then also add spring-boot-starter-web as dependency.
Probably you missing #SpringBootApplication in your spring boot starter class.
#SpringBootApplication
public class LoginSecurityAppApplication {
public static void main(String[] args) {
SpringApplication.run(LoginSecurityAppApplication.class, args);
}
}
The solution is:
I explicitly set the below property to none in application.yml file.
spring:
main:
web-application-type: none
My solution had to do with a bad dependency. I had:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
In my pom and I had to comment out the exclusion to get it working. It must look for this tomcat package for some reason.
In my case the issue resolved on commenting the tomcat dependencies exclusion from spring-boot-starte-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions> -->
</dependency>
You probably use this in your project:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
in which case you'll have to also add:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
and the magic happens :)
PS: that's because Spring will use by default web-MVC instead of web-flux when both are available
Adding following bean worked for me.
#Bean
public ServletWebServerFactory servletWebServerFactory() {
return new TomcatServletWebServerFactory();
}
I was running non web spring application using SpringApplication.run(MyApplication.class, args); without #SpringBootApplication annotation.
Annotate class public static void main with, for example: #SpringBootApplication
To convert an Spring boot wen application to standalone:
Either configure application.properties:
spring.main.web-application-type=none
Or Update application context with NONE web context.
ApplicationContext ctx = new SpringApplicationBuilder(MigrationRunner.class)
.web(WebApplicationType.NONE).run(args);
Using application context, you can get your beans:
myBean bean = (MyBean) ctx.getBean("myBean", MyBean.class);
bean.call_a_method(..);
I had this problem during migration to Spring Boot. I've found a advice to remove dependencies and it helped. So, I removed dependency for jsp-api Project had. Also, servlet-api dependency has to be removed as well.
compileOnly group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2'
As for me, I removed the provided scope in tomcat dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope> // remove this scope
</dependency>
I did right click on my project in IntelliJ IDEA then Maven -> Reload project, problem solved.
In case you're using IntelliJ and this is happening to you (like it did to my noob-self), ensure the Run setting has Spring Boot Application and NOT plain Application.
I was getting same error while using tomcat-jasper newer version
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>10.0.6</version>
</dependency>
I replaced with the stable older version it worked fine for me.
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.46</version>
</dependency>
Apart from the possible solutions in other answers, it is also possible that somehow Maven dependency corruption has occurred on your machine. I was facing the same error on trying to run my (Web) Spring boot application, and it got resolved by running the following -
mvn dependency:purge-local-repository -DreResolve=true
followed by
mvn package
I came onto this solution looking into another issue where Eclipse wouldn't let me run the main application class from the IDE, due to a different error, similar to one on this SO thread -> The type org.springframework.context.ConfigurableApplicationContext cannot be resolved. It is indirectly referenced from required .class files
Similar to the solution of making sure org.springframework.boot:spring-boot-starter-tomcat was installed, I was missing org.eclipse.jetty:jetty-server from my build.gradle
org.springframework.boot:spring-boot-starter-web needs a server be it Tomcat, Jetty or something else - it will compile but not run without one.
I wanted to run the WAR type spring boot application, and when I was trying to run the app as spring boot application I was getting above error. So declaring the web application type in application.properties has worked for me.
spring.main.web-application-type=none
Possible web application type:
NONE - the application should not run as a web application and should not start an embedded web server.
REACTIVE - the application should run as a reactive web application and should start an embedded reactive web server.
SERVLET - the application should run as a servlet-based web application and should start an embedded servlet web server.
In my case, the problem was I didn't had a Tomcat server separately installed in my eclipse. I assumed my Springboot will start the server automatically within itself.
Since my main class extends SpringBootServletInitializer and override configure method, I definitely need a Tomcat server installed in my IDE.
To install, first download Apachce Tomcat (version 9 in my case) and create server using Servers tab.
After installation, run the main class on server.
Run As -> Run on Server
I was trying to create a web application with spring boot and I got the same error.
After inspecting I found that I was missing a dependency. So, be sure to add following dependency to your pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Missing dependency could be cause of this issue
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
I encountered this problem when attempint to run my web application as a fat jar rather than from within my IDE (IntelliJ).
This is what worked for me. Simply adding a default profile to the application.properties file.
spring.profiles.active=default
You don't have to use default if you have already set up other specific profiles (dev/test/prod). But if you haven't this is necessary to run the application as a fat jar.
Upgrading spring-boot-starter-parent in pom.xml to the latest version fixed it for me.
In my case, I was using an TOMCAT 8 and updating to TOMCAT 9 fixed it:
<modelVersion>4.0.0</modelVersion>
<groupId>spring-boot-app</groupId>
<artifactId>spring-boot-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<tomcat.version>9.0.37</tomcat.version>
</properties>
Related issues:
https://github.com/spring-projects/spring-boot/issues?q=missing+ServletWebServerFactory+bean
https://github.com/spring-projects/spring-boot/issues/22013 - Spring Boot app as a module
https://github.com/spring-projects/spring-boot/issues/19141 - Application fails to load when main class extends a base class annotated with #SpringBootApplication when spring-boot-starter-web is included as a dependency
My problem was the same as that in the original question, only that I was running via Eclipse and not cmd. Tried all the solutions listed, but didn't work. The final working solution for me, however, was while running via cmd (or can be run similarly via Eclipse). Used a modified command appended with spring config from cmd:
start java -Xms512m -Xmx1024m <and the usual parameters as needed, like PrintGC etc> -Dspring.config.location=<propertiesfiles> -jar <jar>
I guess my issue was the spring configurations not being loaded correctly.
In my case, the gretty plugin (3.0.6) was still active. Gretty somehow influences the embedded tomcat dependency. Removing gretty fixed the error
Just comment the provided like below

Cannot include Prometheus metrics in spring boot 2 (version 2.0.0.M7)

Cannot include Prometheus metrics in spring boot 2 (version 2.0.0.M7) project.
According micrometer docs added spring-boot-starter-actuator dependency and in application.yaml added management.endpoints.web.expose: prometheus but when calling /actuator/prometheus get
{
"timestamp": 1518159066052,
"path": "/actuator/prometheus",
"message": "Response status 404 with reason \"No matching handler\"",
"status": 404,
"error": "Not Found"
}
Tell me please why I wasn't getting prometheus metrics?
I had trouble initiating micrometer with Springboot 2. x.
These changes in my project helped me to expose the metrics atactuator/prometheus endpoint
These are the changes in my application.properties file
management.endpoints.web.exposure.include=*
management.endpoint.metrics.enabled=true
My build.gradle file included
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('io.micrometer:micrometer-registry-prometheus')
Edit: Since I gave this answer a lot has changed. It was valid for 2.0.0.RC1. Please read the documentation https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html
If above solution doesn't work for someone try this:
I had the same issue with Spring Boot 2.0.0.RC1, spring-boot-starter-web and of course spring-boot-starter-actuator.
My application.properties file read:
management.endpoints.web.expose=prometheus,metrics,info,health
In my pom file I had additionally:
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>0.12.0.RELEASE</version>
</dependency>
Prometheus metrics under /actuator/prometheus where only shown after I had switched to the newest version of micrometer-registry-prometheus:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.0.0-rc.9</version>
</dependency>
Did you add micrometer-registry-prometheus to your dependecies?
Micrometer has a pluggable architecture where you need to define (by plugging dependencies) what monitoring system you'd like to work with. (You can even add multiple, not just one.)
Btw, you should be switching to Spring Boot 2.0.0.RC1. That's the current one as of this writing.
Recap:
pom.xml
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
</parent>
... ... ...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
application.properties.yml (aka. application.yml)
management.endpoints.web.exposure.include=health,info,prometheus
management.endpoint.metrics.enabled=true
management.endpoint.prometheus.enabled=true
management.endpoint.health.show-details=always
management.endpoint.health.show-components=always
management.endpoint.health.probes.enabled=true
management.metrics.tags.application=TheApplicationName
After applying all the above, health & info endpoints were displayed in the http://localhost:8080/actuator -list, but not the prometheus endpoint.
Of course, rerun the Maven lifecycle - still failing prometheus to be displayed ...
Finally my issue disappeared after invalidate caches and restart of my IntelliJ IDE

Spring Boot + Spring Data on GAE with CloudSql - Database Connection

I'm unable to deploy a Spring Boot + Spring Data application to Google App Engine and have the app connect to a Google CloudSql (MySQL) database.
The GAE application and CloudSql instance are in the same project.
I'm able to run the entire app and connect to CloudSql from localhost with no issue.
Spring Boot without a database connection works correctly without error on GAE. Adding Spring Data throws a "com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure" error.
In the pom.xml I have included:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.2</version>
</dependency>
Also, I've tried to use only the mysql dependency:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
In my research, I have not found any good examples or related issues to Spring Boot + Spring Data + GAE + CloudSql.
What are the correct settings for connecting to a CloudSql DB in the application.properties file? I've tried the following, but all threw various errors:
spring.jpa.hibernate.ddl-auto:none
# tried with GoogleDriver and without:
# spring.datasource.driver-class-name=com.mysql.jdbc.GoogleDriver
# urls configs attempted:
# spring.datasource.url=jdbc:mysql://google/**db-name**?cloudSqlInstance=***instance-name***b&socketFactory=com.google.cloud.sql.mysql.SocketFactory
# spring.datasource.url=jdbc:mysql://***instance-name***/**db-name**?user=root
# spring.datasource.url=jdbc:mysql://localhost:3306/**db-name**
spring.datasource.url=jdbc:mysql://127.0.0.1:3306:3306/**db-name**
# spring.datasource.url=jdbc:mysql://***mysql ip address***:3306/**db-name**
spring.datasource.username=***user***
spring.datasource.password=***password***
I had the same issue, below properties in Spring boot application.properties worked for me.
database=mysql
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://google/{db-schema}?socketFactory=com.google.cloud.sql.mysql.SocketFactory&cloudSqlInstance={Cloud-sql-Instance-Name}
spring.datasource.username=root
spring.datasource.password=password
You have to follow following steps :
1) Enable Cloud SQL API by going to following link:
https://console.cloud.google.com/flows/enableapi?apiid=sqladmin&redirect=https:%2F%2Fconsole.cloud.google.com
2) Download service account JSON key file from https://console.cloud.google.com/apis/credentials/serviceaccountkey
3) Set this filepath as environment variable with key GOOGLE_APPLICATION_CREDENTIALS.
on Windows OS open command prompt and run command: set GOOGLE_APPLICATION_CREDENTIALS=[File path]
on Linux OS on console run command : export GOOGLE_APPLICATION_CREDENTIALS=[File path]
4) Add following dependencies in pom.xml :
`<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.23.0</version>
</dependency>
`
5) Use following properties in application.properties file :
spring.datasource.url=jdbc:mysql://google/[Database-Name]?cloudSqlInstance=[instance-Connection-Name]&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false
spring.datasource.username=[Database-Username]
spring.datasource.password=[Database-Password]
Hope this helps.

Resources