Spring Cloud GCP Cloud Spanner Starter Example
This code sample demonstrates how to read and write POJOs from Google Cloud Spanner using the Spring Data Cloud Spanner module Spring Cloud GCP Cloud Spanner Starter.
Running the example
Create a new Cloud Spanner instance named "spring-demo". The example application will create the database and tables if they do not already exist.
If you are authenticated in the Cloud SDK, your credentials will be automatically found by the Spring Boot Starter for Google Cloud Spanner.
Alternatively, create a service account from the Google Cloud Console and download its private key.
Then, uncomment the
spring.cloud.gcp.spanner.credentials.locationproperty in the application.properties file and fill its value with the path to your service account private key on your local file system, prepended with
SpannerTemplateExampleSpring Boot console apps. This causes the app to generate sample
Tradeobjects filled with some demo data and persists them to the Cloud Spanner table created earlier. These POJOs are then retrieved and their contents are printed in the browser.
To run the SpannerRepositoryExample:
`mvn spring-boot:run -Dspring-boot.run.arguments=--spanner_repository`
To run the SpannerTemplateExample:
`mvn spring-boot:run -Dspring-boot.run.arguments=--spanner_template`
Note that the sample application, still running, also provides REST endpoints.
For example, you can retrieve all
Trade objects in the repository by using
curl http://localhost:8080/trades, or any specific trade via
This is achieved using Spring Data REST and the