Application Development

Announcing Spring Cloud GCP 1.1: deepening ties to Pivotal’s Spring Framework

Here at Google we have been working hard with Pivotal’s Spring team to integrate the Spring Framework and Google Cloud Platform (GCP). Last summer, we released the first official 1.0 version of the project, providing the general availability of idiomatic Spring support for Cloud Pub/Sub, Cloud SQL, Stackdriver Trace, Stackdriver Logging, and Cloud Storage.

Since then, we’ve accelerated development of the integrations with help from the community. We’ve listened to your feedback on our GitHub issue tracker and at conferences like Google Cloud Next and Spring One Platform. As a result, we started tackling more complex integrations like Spring Data and Spring Security.


Today, we’re announcing another major GA release 1.1 with many new integrations, bug fixes, and documentation updates. Despite the minor version increase from 1.0 to 1.1, it’s a big milestone for the project!

What’s New in Spring Cloud GCP 1.1?

Joining the Spring Cloud release train

The project has recently joined the Spring Cloud release train, which means that it is completely aligned with the Spring Cloud schedule. In fact, Spring Cloud GCP is being released together with the rest of Spring Cloud projects today. Consequently, just like the rest of the Spring Cloud Greenwich release, Spring Cloud GCP 1.1 is compatible with Spring Boot 2.1 and Java 11, and it includes all of the goodness of the most recent Spring Boot version.

Spring Data support for Cloud Spanner and Cloud Datastore

We’re really excited to include Spring Data support for Cloud Spanner and Cloud Datastore in this release. We focused intensely on these integrations with the Spring Data team last year, and are excited to make them generally available via today’s release.

In case you haven’t tried Spring Data before, it’s one of the best examples of how Spring can help you eliminate boilerplate code and let you focus on writing the business logic. No more having to write boring data persistence (CRUD) code! It does so using object-relational mapping and automatic query generation, while still giving you the flexibility to take advantage of the special features of each database.

With Spring Data, Cloud Spanner and Cloud Datastore can accommodate a wide range of object and data relationships, but they have distinct specialities. Cloud Spanner supports composite keys and interleaved tables representing strict parent-child relationships. On the other hand, Cloud Datastore’s document-style entities support single-field IDs only but allow free-form relationships.

Here’s a Spring Data Cloud Spanner example:

  <dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-gcp-starter-data-spanner</artifactId>
</dependency>
  @SpringBootApplication
public class SpannerApplication {
@Autowired
private ClassroomRepository repo;
 
public void main() {
		List<Classroom> allRooms = repo.findAll();
	List<Chair> chairsInRoomA = repo.findById(“ClassroomA”).get().chairs;
}
}
 
@Table(name = "classrooms")
class Classroom {
@PrimaryKey
@Column(name = "classroom_id")
String classroomId;
 
       @PrimaryKey(keyOrder = 2)
       @Column(name = "school_id")
       long schoolId;
 
	@Interleaved
	List<Chair> chairs;
}
 
interface ClassroomRepository extends SpannerRepository<Classroom, Key> {
}

A complete Spring Data Cloud Spanner sample application is available in our GitHub repository.

Here’s a Spring Data Cloud Datastore example:

  <dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-gcp-starter-data-datastore</artifactId>
</dependency>
  @SpringBootApplication
public class DatastoreApplication {
@Autowired
private ClassroomRepository repo;
 
public void main() {
		List<Classroom> allRooms = repo.findAll();
	List<Chair> chairsInRoomA = repo.findById(“UniqueClassroomA”).get().chairs;
}
}
 
@Entity(name = "classrooms")
class Classroom {
       @Id
       @Field(name = "unique_classroom_id")
       String uniqueClassroomId;
 
	@Descendants
	List<Chair> chairs;
 
	List<Table> tables;
 
@Reference
	Students[] students;
}
 
interface ClassroomRepository extends DatastoreRepository<Classroom, String> {
}

Again, you can find a complete Spring Data Cloud Datastore sample application in our GitHub repository. You can also walk through a codelab for a hands-on experience.

Spring Security for Cloud IAP

Cloud Identity-Aware Proxy (IAP) provides a security layer over applications deployed to Google Cloud. The proxy verifies user identity and includes it in the secure x-goog-iap-jwt-assertion HTTP header that is passed to your application. Our integration with Spring Security makes extracting identity from Cloud IAP as simple as pulling in a starter dependency:

  <dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-gcp-starter-security-iap</artifactId>
</dependency>

And since Cloud IAP integrates seamlessly with Spring Security, you can restrict actions within the application to specific users using Spring Security customizations.

You can learn more about it by checking out our sample application.

Kotlin support

Kotlin is a general purpose programming language used primarily for mobile development. Last year, we started to hear increasing interest from the Java community in seeing Kotlin supported in Spring. We saw numerous well-attended Kotlin sessions at the SpringOne Platform conference and it came up more than once in informal conversations. We took notice and created a sample Spring Cloud GCP application in Kotlin to get you started with using Kotlin on GCP with Spring.

The latest version of the Spring Framework (5.1) already has first-class Kotlin support along with new features like refinements to the Kotlin beans domain-specific language. We built on that support. In the process of creating the sample Kotlin application, we experienced how easy it is to start using Kotlin for Spring Boot applications. Check out the sample registration service application written in Kotlin, or try building it step-by-step in this codelab.

Analyze images with Cloud Vision

We created a Spring Boot starter that makes it really easy to start analyzing images using Google’s pre-trained Cloud Vision machine learning model. You can identify objects in images and extract text with only a few lines of code in your Spring Boot applications. We introduced the VisionTemplate class to make the most common image analysis tasks even easier than by using the Cloud Vision API directly.

  @Autowired
private ResourceLoader resourceLoader;
 
@Autowired
private CloudVisionTemplate cloudVisionTemplate;
 
public void classifyImage(String imageUrl) {
	AnnotateImageResponse response = this.cloudVisionTemplate.analyzeImage(
           this.resourceLoader.getResource(imageUrl), Type.LABEL_DETECTION);
 
	System.out.println("Your image has been classified with the following labels: ");
	for (EntityAnnotation annotation : response.getLabelAnnotationsList()) {
		System.out.println(annotation.getDescription());
	}
}
 
public void extractText(String imageUrl) {
	String textFromImage = this.cloudVisionTemplate.extractTextFromImage(
			this.resourceLoader.getResource(imageUrl));
	System.out.println("The text extracted from the image is: ");
	System.out.println(textFromImage);
}

What’s next?

Community feedback is invaluable. Last year you told us you wanted more Spring Data integrations, Spring Security support, and Kotlin compatibility, and we included a lot of this in the 1.1 release. We’ll continue to bring more on these fronts and plan to provide more support for Reactive and Spring Data JPA with Hibernate in upcoming releases. Tell us what you want to see in the next version of Spring Cloud GCP.  We welcome feature requests and questions on our GitHub issue tracker, too.

Learn more

Josh Long from the Spring team recently wrote an excellent 8-part tutorial series for Spring Cloud GCP. Our own Ray Tsang and Josh also gave a great talk at GCP Next ‘18 demonstrating many of the project’s features. You can also learn more in our reference documentation and connect with us on GitHub.


This release would not have been possible without the Spring Team at Pivotal, the Cloud Java Frameworks team at Google, and numerous other contributors to the project on Github. A special thanks to Elena Felder, Dan Zou, Chengyuan Zhao, Dmitry Solomakha, Ray Tsang, Artem Bilan for being the core contributors to the project and helping to write this post.