The Distributed SQL Blog

Thoughts on distributed databases, open source and cloud native

Spring Data REST Services Powered By Distributed SQL – A Hands-on Lab

The Spring application development framework is arguably the most popular framework among Java developers. However, given its extensive breadth and depth, it can be difficult to learn for new users. As the name suggests, Spring Boot makes it easy to boot up with the Spring framework. It shortens development time by taking an opinionated view of the framework and the associated third-party libraries. Annotation configuration and default codes are two examples of such a view. The result is rapid application development with near zero configuration.

Spring Data is a collection of projects in the Spring framework that allow Spring apps to manage data in SQL and NoSQL databases. This hands-on lab shows how a distributed SQL database like YugaByte DB can be used to power a Spring Boot REST service using the Spring Data JPA module. YugaByte DB’s PostgreSQL-compatible YSQL API is used as the database client API in this lab.

Prerequisites

This tutorial assumes that you have installed JDK version 1.8+ and Maven 3.3+

Install YugaByte DB

Get the sample app code

Clone the orm-examples GitHub repo as shown below.

This repository has a Spring Boot example that implements a simple REST API server. The API is essentially the server-side of an e-commerce application. Database access in this application is managed through Spring Data JPA which internally uses Hibernate as the default JPA provider. The application manages the following entities:

  • The users of the e-commerce site are stored in the users table.
  • The products table contains a list of products the e-commerce site sells.
  • The orders placed by the users are populated in the orders table. An order can consist of multiple line items, each of these are inserted in the orderline table.

The source for the above application can be found in this repo.

There are a number of options that can be customized in the properties file located at src/main/resources/application.properties. Given YSQL’s compatibility with the PostgreSQL language, the spring.jpa.database property is set to POSTGRESQL and the spring.datasource.url is set to the YSQL JDBC url jdbc:postgresql://localhost:5433/postgres.

Build & run the app

Build the app.

Run the REST API server at http://localhost:8080

Send requests to the app

Create 2 users.

Create 2 products.

Create 2 orders.

Query results

Using the YSQL shell

Repeat the above SELECT count (*) queries on the products and orders tables as well. Confirm that 2 rows are returned for each of those queries.

Using the REST API

Repeat the above curl commands on the products (http://localhost:8080/products) and orders (http://localhost:8080/orders) resources as well. Confirm that 2 rows are returned for each of those queries.

What’s Next?

  • Compare YugaByte DB in depth to databases like CockroachDB, Google Cloud Spanner and MongoDB.
  • Get started with YugaByte DB on macOS, Linux, Docker, and Kubernetes.
  • Contact us to learn more about licensing, pricing or to schedule a technical overview.

Related Posts