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
This tutorial consists of three parts:
Part 1: Creating Spring Boot REST API project and project structure
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.
1. Go to File > New Project... and under "Spring Boot" category pick "Spring Starter Project".
In this example, we'll name project spring-boot-demo-project, add group name com.javahowtos and package com.javahowtos.demo.
On the next page, pick Spring Boot version, I'll choose 2.2.0 M4. Click finish, and you're done here.
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.
That's it for Part 1. Now you're ready to do some coding. :)