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.


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.

$ git clone

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/ 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.

$ cd ./java/spring
$ mvn -DskipTests package

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

$ mvn spring-boot:run

Send requests to the app

Create 2 users.

$ curl --data '{ "firstName" : "John", "lastName" : "Smith", "email" : "[email protected]" }' \
   -v -X POST -H 'Content-Type:application/json' http://localhost:8080/users

$ curl --data '{ "firstName" : "Tom", "lastName" : "Stewart", "email" : "[email protected]" }' \
   -v -X POST -H 'Content-Type:application/json' http://localhost:8080/users

Create 2 products.

$ curl \
  --data '{ "productName": "Notebook", "description": "200 page notebook", "price": 7.50 }' \
  -v -X POST -H 'Content-Type:application/json' http://localhost:8080/products

$ curl \
  --data '{ "productName": "Pencil", "description": "Mechanical pencil", "price": 2.50 }' \
  -v -X POST -H 'Content-Type:application/json' http://localhost:8080/products

Create 2 orders.

$ curl \
  --data '{ "userId": "2", "products": [ { "productId": 1, "units": 2 } ] }' \
  -v -X POST -H 'Content-Type:application/json' http://localhost:8080/orders

$ curl \
  --data '{ "userId": "2", "products": [ { "productId": 1, "units": 2 }, { "productId": 2, "units": 4 } ] }' \
  -v -X POST -H 'Content-Type:application/json' http://localhost:8080/orders

Query results

Using the YSQL shell

$ ./bin/ysqlsh 
ysqlsh (11.2)
Type "help" for help.


postgres=> SELECT count(*) FROM users;

(1 row)

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

$ curl http://localhost:8080/users

  "content": [
      "userId": 2,
      "firstName": "Tom",
      "lastName": "Stewart",
      "email": "[email protected]"
      "userId": 1,
      "firstName": "John",
      "lastName": "Smith",
      "email": "[email protected]"

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