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?
In Eclipse,
File -> New -> Spring Starter Project
as shown below.

After creating project let’s code!
1- Model Package
Firstly, we’re starting with creating model classes. My topic is Student-Course relation so I create two model classes that are Student.java and Course.java. In this model students has a course list.
Student.java
Course.java
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).
StudentRepo.Java
CourseRepo.java
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.
StudentService.java
CourseService.java
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.
StudentController.java
CourseController.java

Let’s Run The Application and Test HTTP Requests
- Student in JSON Format:
{
"id": 125,
"studentName": "Arda",
"courses":[
{
"courseName": "Math"
}
]
}
- Course in JSON Format:
{
"courseName": "English"
}
1- Post Method
Endpoints:

2-Get Method
Endpoints:
- http://localhost:8080/list/students
- http://localhost:8080/list/students/{id}
- http://localhost:8080/list/courses
- http://localhost:8080/list/courses/{courseName}

3-Put Method
Endpoint:

4-Delete Method
Endpoints:
- http://localhost:8080/delete/students
- http://localhost:8080/delete/students/{id}
- http://localhost:8080/delete/courses
- http://localhost:8080/delete/courses/{courseName}

You can test the other endpoints. You can also change the endpoints and make practise.
GitHub Repo is here: