Bootiful GCP: Use Spring Cloud GCP to Connect to Other GCP Services (7/8)

Engineering | Josh Long | September 09, 2018 | ...

Hi Spring fans! In this brief 8 part series we’re going to look at the Spring Cloud integration for Google Cloud Platform, called Spring Cloud GCP. Spring Cloud GCP represents a joint effort between Google and Pivotal that endeavors to provide a first class experience for Spring Cloud developers when using the Google Cloud Platform. Pivotal Cloud Foundry users will enjoy an even easier integration with the GCP service broker. I wrote these installments with input from Google Cloud Developer Advocate, and my buddy, Ray Tsang. You can also catch a walkthrough of Spring Cloud GCP in our Google Next 2018 session, Bootiful Google Cloud Platform. Thanks buddy! As always, I'd love to hear from you if you have feedback.

There are eight posts in the series. Here they all are:


  • [Bootiful GCP: Use Spring Cloud GCP to Connect to Other GCP Services (7/8)


The Spring Cloud GCP project strives to provide integrations with Spring and some of the GCP services that map well to Spring. But GCP is vast! There are a good deal many other services out there that you can consume via their direct Java SDK or even through their REST APIs, directly. Spring Cloud GCP can make working with those APIs a bit easier, too! In this section, we’re going to integrate with the Google Cloud Vision API which supports analyzing images and doing feature detection.

As always, you will need to enable the API:

gcloud services enable

When you use the auto-configurations in Spring Cloud GCP they conveniently obtain the required OAuth scopes to work with a given API on your behalf, and you never need to worry about it. We’ll need to do this work ourselves for other services. This is easy enough, thankfully. Use the property to obtain a general, platform-wide, catch-all scope that can be used to request permission for all basic Google Cloud Platform APIs.


And.. that’s it! Now you can use the API as you like. Let’s standup a simple REST API to which you can post an image as a multipart file upload and have the Google Cloud Vision API do feature detection.


import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;

// curl -F "[email protected]$HOME/Pictures/soup.jpg" http://localhost:8080/analyze
public class VisionApplication {

        ImageAnnotatorClient imageAnnotatorClient(
            CredentialsProvider credentialsProvider) throws IOException {
                ImageAnnotatorSettings settings = ImageAnnotatorSettings
                return ImageAnnotatorClient.create(settings);

        public static class ImageAnalyzerRestController {

                private final ImageAnnotatorClient client;

                private final Feature labelDetection = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
                private final Feature textDetection = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build();

                ImageAnalyzerRestController(ImageAnnotatorClient client) {
                        this.client = client;

                String analyze(@RequestParam MultipartFile image) throws IOException {
                        byte[] data = image.getBytes();
                        ByteString imgBytes = ByteString.copyFrom(data);
                        Image img = Image.newBuilder().setContent(imgBytes).build();

                        AnnotateImageRequest request = AnnotateImageRequest
                        BatchAnnotateImagesResponse responses = this.client
                        AnnotateImageResponse reply = responses.getResponses(0);
                        return reply.toString();

        public static void main(String args[]) {
      , args);
  • we’re configuring the Google Cloud Vision client manually. This is more work than you might do if you had a Spring Boot starter, but it’s definitely not bad!
  • what kind of analysis do we want the client to do?
  • Spring MVC can turn multipart file uploads into a MultipartFile from which we can easily extract bytes to feed into this API.

You can POST an image to this endpoint using curl or any other general purpose HTTP client. Here’s how it would work with:


curl  -F "[email protected]/home/jlong/Desktop/soup.jpg" http://localhost:8080/analyze

There are a zillion other APIs with whom you might work! Here we are only just beginning to scratch the surface of what’s possible. Check out this service catalog! There are things like Google Cloud DataStore, Google Storage, Firebase, BigQuery, Apigee, video streaming services, IoT services, machine learning, Google Tensorflow, Google Dataflow, Google Cloud AutoML, Cloud Natural Language, Cloud Speech-to-Text, Cloud Text-to-Speech, Genomics APIs, Video Intelligence, and so much more.

Get the Spring newsletter

Thank you!

Get ahead

VMware offers training and certification to turbo-charge your progress.

Learn more

Get support

Spring Runtime offers support and binaries for OpenJDK™, Spring, and Apache Tomcat® in one simple subscription.

Learn more

Upcoming events

Check out all the upcoming events in the Spring community.

View all