This project is a simple Rest API with Spring Boot CRUD Appliacation.
In Eclipse,
File -> New -> Spring Starter Project
as shown below.
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 byDomain-Driven Design (Evans, 2003) as "a mechanism for encapsulating storage,retrieval, and search behavior which emulates a collection of objects". This project we're not using 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).
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 themodel, with no encapsulated state." This classes has variables. The variables' types are from repository 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. See StudentController.java.
Note: I use Postman to send request.
- Student in JSON Format:
{
"id": 125,
"studentName": "Arda",
"courses":[
{
"courseName": "Math"
}
]
}
- Course in JSON Format:
{
"courseName": "English"
}
Endpoints:
Endpoints:
- http://localhost:8080/list/students
- http://localhost:8080/list/students/{id}
- http://localhost:8080/list/courses
- http://localhost:8080/list/courses/{courseName}
Endpoint:
Endpoints: