
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
In this series of labs, you take a demo microservices Java application built with the Spring framework and modify it to use an external database server. You adopt some of the best practices for tracing, configuration management, and integration with other services using integration patterns.
In addition to the integration you have seen in previous labs using Spring Boot starters, Google Cloud offers many other APIs that you can use directly from your Java applications.
Google Cloud has a set of ready-to-use, idiomatic Java client libraries called google-cloud-java
. You can consume any of the client libraries for google-cloud-java
even without a Spring Boot starter.
In this lab, you modify the application to use Cloud Vision API to analyze the images uploaded by you.
Your Spring Cloud GCP project does not have a Spring Boot starter for Vision API. So, in this lab you integrate client libraries without using a Spring Cloud GCP Spring Boot starter. You also configure and use a service account to provide your application with the correct permissions to access Vision API.
Vision API enables developers to understand the content of an image by encapsulating powerful machine-learning models in an easy-to-use REST API. Vision API quickly classifies images into thousands of categories, detects individual objects and faces within images, and reads printed words contained in images. You can build metadata on your image catalog, moderate offensive content, or enable new marketing scenarios through image sentiment analysis. Vision API enables your applications to easily detect broad sets of objects in your images. Images can range from flowers, animals, or transportation to thousands of other object categories commonly found in images.
In this lab, you learn how to perform the following tasks:
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is a panel populated with the temporary credentials that you must use for this lab.
Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Choose an account page.
On the Choose an account page, click Use Another Account. The Sign in page opens.
Paste the username that you copied from the Connection Details panel. Then copy and paste the password.
After a few moments, the Cloud console opens in this tab.
After you complete the initial sign-in steps, the project dashboard appears.
In this task, you clone the source repository files that are used throughout this lab.
To begin the lab, click the Activate Cloud Shell button at the top of the Google Cloud Console.
To activate the code editor, click the Open Editor
button on the toolbar of the Cloud Shell window. Editor opens in a new tab with continued access to Cloud Shell.
Now you're ready to go!
In this task, you enable the Vision API so that you can use it to analyze uploaded images.
In this task, you add the Vision client library to the guestbook frontend application.
~/guestbook-frontend/pom.xml
.<dependencies>
section, just before the closing </dependencies>
tag:In this task, you specify the Google Cloud scope in the application.properties
file.
Without customization, the Spring Cloud GCP starters request permission scopes to use APIs that the starters integrate with. Because you use a new API that is not integrated with the starter, you must specify the scope. An all-purpose scope can be used to request permission for all basic Google Cloud APIs.
In the Cloud Shell code editor, open ~/guestbook-frontend/src/main/resources/application.properties
.
Add the following entry:
The application.properties file should contain the following properties:
In this task, you analyze the uploaded image, label the objects in the image, and print the response.
Given an image, Vision API can identify objects, landmarks, the location of faces, and facial expressions. It can also extract text and evaluate whether the image is considered safe.
You add a method to FrontendController.java
that sends an image to Google Vision API for analysis.
~/guestbook-frontend/src/main/java/com/example/frontend/FrontendController.java
.import
directives immediately below the existing imports:FrontendController
class definition immediately above the @GetMapping("/")
line:In FrontendController.java
you add a call to the new analyzeImage
method after the code that uploads the file to Cloud Storage.
~/guestbook-frontend/src/main/java/com/example/frontend/FrontendController.java
.post
method definition after the try
block inside the post
method definition:The post
method definition should look like the screenshot:
In this task, you create a service account with the Owner role, and you create a JSON file containing the authentication keys for the service account.
To make calls to the Vision API from your application, you need a service account with sufficient permissions.
This command creates Service Account Credentials that are stored in the $HOME/service-account.json
file.
service-account.json
file as your own username/password. Do not share this information.
In this task, you run the application components in Cloud Shell to test the new Vision API functionality. When you start the frontend application, you use the new service account user credential so that the frontend application can authenticate with Vision API.
guestbook-service
directory:The backend service application launches on port 8081 and takes a minute or two to complete. Wait until you see that the GuestbookApplication is running.
guestbook-frontend
directory:In this lab, you added a Google Cloud API Java library to an application. You created a Google Cloud credential scope for Spring. You also created a Java bean that implements Vision API features. Finally, you used the Vision API to add image analysis to an application.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one