Create a Rest API with Spring Boot
Hi there, my name is Muzaffer Arda Uslu. I am a computer engineer student at Eskişehir Osmangazi University. I have been learning JAVA for two months. Now, I’m gonna try to explain how to create a simple Spring Boot Project.
Main Goal is:
- Create a Spring Boot Project
- To learn using annotations such as @Repository, @Service, @RestController, @GetMapping, @PostMapping, @DeleteMapping, @PutMapping
How to Create a Spring Starter Project?
File -> New -> Spring Starter Project
as shown below.
After creating project let’s code!
1- Model Package
2- Repository Package
After that, we’re creating Repository classes. So let’s learn @Repository annotation. @Repository annotation indicates that an annotated class is a “Repository”, originally defined by Domain-Driven Design (Evans, 2003) as “a mechanism for encapsulating storage,retrieval, and search behavior which emulates a collection of objects”. This project, we don’t use DataBase so we have two private variables that are studentList and courseList. They’re our repository. We’re implementing CRUD operations for the lists(studentList and courseList).
3- Service Package
Now, let’s create Service classes. So, what is @Service annotation? @Service annotation is used with classes that provide some business functionalities. It indicates that an annotated class is a “Service”, originally defined by Domain-DrivenDesign (Evans, 2003) as “an operation offered as an interface that stands alone in the model, with no encapsulated state.” This classes have variables. The variables’ types are from repository package.
4- Controller Package
In this package, we use @RestController annotation. This annotation takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON or XML response.
We can use all HTTP Request with @Restcontroller.
Let’s Run The Application and Test HTTP Requests
- Student in JSON Format:
- Course in JSON Format:
1- Post Method
You can test the other endpoints. You can also change the endpoints and make practise.
GitHub Repo is here: