Over a million developers have joined DZone.

Bootiful GCP: Use Spring Cloud GCP to Connect to Other GCP Services

DZone 's Guide to

Bootiful GCP: Use Spring Cloud GCP to Connect to Other GCP Services

Want to learn more about using Spring Cloud's Google Cloud Platform to connect to other GCP services? Check out this tutorial to learn how!

· Java Zone ·
Free Resource

Hi, Spring fans! In this brief part of the series, we’re going to look at the Spring Cloud integration for Google Cloud Platform, 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. Let's get into it.

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 is a good deal of 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 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 vision.googleapis.com

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 spring.cloud.gcp.credentials.scopes property to obtain a general, platform-wide, catch-all scope that can be used to request permissions 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 that allows you to post an image as a multipart file upload and have the Google Cloud Vision API perform feature detection.

package com.example.gcp.vision;

import com.google.api.gax.core.CredentialsProvider;
import com.google.cloud.vision.v1.*;
import com.google.protobuf.ByteString;
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.io.IOException;
import java.util.Collections;

// curl -F "image=@$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[]) {
                SpringApplication.run(VisionApplication.class, 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 perform?
  • Spring MVC can turn multipart file uploads into a MultipartFile, 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:

curl  -F "image=@/home/jlong/Desktop/soup.jpg" http://localhost:8080/analyze

There are a zillion other APIs with whom might work! Here, we are only just beginning to scratch the surface of what’s out there. You can also check out this service catalog. There are services 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.

java ,spring ,spring boot ,google cloud ,spring cloud ,cloud foundry ,google cloud platform ,gcp

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}