Goal - Spring Boot REST API

 The main goal of this guide is to show you how to create your firs Spring Boot REST API implementing the Service Layer and Repository pattern.

We'll go through the creation of the following components:

  • setting up project
  • one simple entity
  • controller
  • service
  • repository

This tutorial consists of three parts:

Part 1: Creating Spring Boot REST API project and project structure

Part 2: Adding simple entity and CRUD (create, read, update, delete) operations with it

Part 3: Testing using Postman

Project code is available on gitHub.

What you’ll need?

For purposes of this tutorial, you'll need Java JDK installed on your machine - I'm currently using version 1.8.0_222. Also, you'll need some IDE. I recommend using Spring Tools Suite, but you can pick whatever you consider comfortable.

 

Get Gayle Laakmann - Cracking the Coding Interview - 4th, 5th and 6th edition

Create a project

 1. Go to File > New Project... and under "Spring Boot" category pick "Spring Starter Project".

spring boot rest api

In this example, we'll name project spring-boot-demo-project, add group name com.javahowtos and package com.javahowtos.demo.

 

 spring boot rest api

 

On the next page, pick Spring Boot version, I'll choose 2.2.0 M4. Click finish, and you're done here.

spring boot rest api 

 Afterward, you'll see typical Maven project structure with src/main/java, src/main/resources, etc and pom.xml file with dependencies we chose. You'll see in Maven Dependencies folder all included dependencies and one class with annotation @SpringBootApplication.

@SpringBootApplication annotation enables developers to use auto-configuration, component scan and be able to define extra configuration on their "application class".

Under src/main/java, add packages named controller, service, repository and model with classes named ItemController, ItemService and ItemRepository.

spring boot rest api

 

That's it for Part 1. Now you're ready to do some coding. :)

Go to Part 2 ->

 

 


Newsletter

Please enable the javascript to submit this form