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

Sid Choudhury

SVP, Product

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 YugabyteDB can be used to power a Spring Boot REST service using the Spring Data JPA module. YugabyteDB’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 YugabyteDB

Get the sample app code

Clone the orm-examples GitHub repo as shown below.

$ git clone https://github.com/YugaByte/orm-examples.git

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.

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

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

$ mvn spring-boot:run

Send requests to the app

Create 2 users.

$ curl --data '{ "firstName" : "John", "lastName" : "Smith", "email" : "jsmith@yb.com" }' \
   -v -X POST -H 'Content-Type:application/json' https://localhost:8080/users

$ curl --data '{ "firstName" : "Tom", "lastName" : "Stewart", "email" : "tstewart@yb.com" }' \
   -v -X POST -H 'Content-Type:application/json' https://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' https://localhost:8080/products

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

Create 2 orders.

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

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

Query results

Using the YSQL shell

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

postgres=#

postgres=> SELECT count(*) FROM users;

count 
-------
     2
(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 https://localhost:8080/users

{
  "content": [
    {
      "userId": 2,
      "firstName": "Tom",
      "lastName": "Stewart",
      "email": "tstewart@yb.com"
    },
    {
      "userId": 1,
      "firstName": "John",
      "lastName": "Smith",
      "email": "jsmith@yb.com"
    }
  ],
  ...
}

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

What’s Next?

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

SVP, Product

Related Posts

Explore Distributed SQL and YugabyteDB in Depth

Discover the future of data management.
Learn at Yugabyte University
Get Started
Browse Yugabyte Docs
Explore docs
PostgreSQL For Cloud Native World
Read for Free