DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Bootiful GCP: Use Spring Cloud GCP to Connect to Other GCP Services

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!

Josh Long user avatar by
Josh Long
·
Sep. 11, 18 · Tutorial
Like (1)
Save
Tweet
Share
5.48K Views

Join the DZone community and get the full member experience.

Join For Free

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.

src/main/resources/applications.properties.

spring.cloud.gcp.credentials.scopes=https://www.googleapis.com/auth/cloud-platform
spring.cloud.gcp.credentials.encoded-key=FIXME


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
@SpringBootApplication
public class VisionApplication {


        @Bean
        ImageAnnotatorClient imageAnnotatorClient(
            CredentialsProvider credentialsProvider) throws IOException {
                ImageAnnotatorSettings settings = ImageAnnotatorSettings
                    .newBuilder()
                    .setCredentialsProvider(credentialsProvider)
                    .build();
                return ImageAnnotatorClient.create(settings);
        }

        @Slf4j
        @RestController
        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;
                }

                @PostMapping("/analyze")
                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
                            .newBuilder()
                            .addFeatures(this.labelDetection)
                            .addFeatures(this.textDetection)
                            .setImage(img)
                            .build();
                        BatchAnnotateImagesResponse responses = this.client
                            .batchAnnotateImages(Collections.singletonList(request));
                        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.

Spring Cloud Spring Framework

Published at DZone with permission of Josh Long, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Handle Secrets in Docker
  • Fargate vs. Lambda: The Battle of the Future
  • REST vs. Messaging for Microservices
  • Cloud Performance Engineering

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: