This tutorial will walk you through the steps of creating a RESTful API Example with Spring Data REST, JPA, Hibernate, MySQL and Docker.

What you'll need

  • Docker CE 18+

Project structure

Project dependencies


Define JPA Entity


Define Spring Data Repository

Spring Data Rest will auto create RESTful APIs based on domain model and repository, all you have to do in this example is to extend JpaRepository


Application Properties


Run the application


Prepare Dockerfile for Java/Spring Boot application


Start your application and infrastructure via Docker Compose. Make sure your local Docker is running and your bash console is at the springboot-examples project root directory

docker-compose -f docker-compose-restful.yaml up --renew-anon-volumes springdatarest-mysql-single

Access to MySQL Server docker container by issuing below bash command and key in hellokoding on Enter password:

docker exec -it hk-mysql mysql -p

Check the data table schema created by JPA/Hibernate based on your definition on Book.java

Query the data

Book data should be empty now. Let create it with the exposed RESTful APIs.

Test your RESTful APIs with curl

Create some new books

curl -i -X POST -H "Content-Type:application/json" -d "{\"title\" : \"Hello Koding\", \"description\": \"Simple coding examples and tutorials\"}" http://localhost:8080/books
curl -i -X POST -H "Content-Type:application/json" -d "{\"title\" : \"Hello Koding 2\", \"description\": \"Simple coding examples and tutorials 2\"}" http://localhost:8080/books

Find all books

curl http://localhost:8080/books

Find book with id=2

curl curl http://localhost:8080/books/2

Update book id 2

curl -i -X PATCH -H "Content-Type:application/json" -d "{\"title\" : \"Hello Koding 2 updated\"}" http://localhost:8080/books/2

Replace book id 2

curl -i -X PUT -H "Content-Type:application/json" -d "{\"title\" : \"Hello Koding 2 replaced\"}" http://localhost:8080/books/2

Delete book id 2

curl -i -X DELETE http://localhost:8080/books/2

That's it! Thanks for joining and have a happy coding. You can find the full source code as below

Source code

https://github.com/hellokoding/hellokoding-series/tree/master/springboot-examples/restful-api/springdatarest-mysql-single

You may also like
Handling Circular Reference of JPA/Hibernate Entity Bidirectional Relationships with Jackson JsonIgnoreProperties