In this tutorial, you will learn to build a simple REST API web service example in Spring Boot by using Spring Web MVC with JPA, Hibernate and HSQL

What you'll need

  • Your favorite IDE
  • JDK 8+ or OpenJDK 8+
  • Maven 3+

Init project structure

You can create and init a new Spring Boot project by using Spring CLI or Spring Initializr. Learn more about using these tools at here

The final project structure as below

├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── hellokoding
│       │           └── springboot
│       │               └── restful
│       │                   ├── product
│       │                   │   ├── Product.java
│       │                   │   ├── ProductAPI.java
│       │                   │   ├── ProductRepository.java
│       │                   │   └── ProductService.java
│       │                   └── Application.java
│       └── resources
│           └── application.properties
└── pom.xml

Project dependencies

Add Spring Web MVC into your project as a dependency on pom.xml or build.gradle file. In Spring Boot project, you can add the dependency spring-boot-starter-web. The library versions can be omitted as it is resolved by the parent pom provided by Spring Boot

<dependency>  
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>  

We also need Spring Data JPA and HSQL Java Client dependencies for working with the HSQL database

<dependency>  
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>  
<dependency>  
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
</dependency>  

You can find the full pom.xml file as below


Define JPA Entity and Repository



ProductRepository extends Spring Data JPA's JpaRepository to provide the common CRUD functions such as findAll, findById, saveAll, save, deleteById to the underlying database

Define Service and REST APIs



In Spring, you can use @Controller or @RestController class annotation to indicate a handler for HTTP requests

@RestController is a shorthand for @Controller and @ResponseBody

@ResponseBody indicates handler methods return value will be serialized as JSON and bind to the web response body

@RequestMapping class annotation maps HTTP requests with end point /api/v1/products to all handler methods in ProductAPI class

@RequestBody indicates HTTP request body in JSON string format will be converted to the parameter object

@GetMapping, @PostMapping, @PutMapping, @DeleteMapping indicate HTTP GET, POST, PUT, DELETE request handlers respectively

@PathVariable maps the handler parameter with the URI variable

Config and Run

Application Configurations


Run the application with JDK/OpenJDK and Maven

Type the below command at the project root directory

mvn clean spring-boot:run

Test your REST APIs with curl

Create a new product

curl -i -H "Content-Type: application/json" -X POST -d '{"name":"Hello Koding","description": "Simple coding examples and tutorials","price":"1"}' http://localhost:8080/api/v1/products

Find all products

curl -i http://localhost:8080/api/v1/products

Source code

https://github.com/hellokoding/hellokoding-courses/tree/master/springboot-examples/springboot-restapi