This is a hands-on project from the Spring Boot Tutorial by Java Brains!
With the development process of the Course Management System detailed below, I attempt to condense and provide some insight into what I have learnt throughout this course.
- Create a Maven project.
- Add
spring-boot-starter-web
dependency. - Add
CourseApiApp
class and run the application.
- Add
Topic
class withid
,name
, anddescription
. - Add Topic RestController.
- Add
getAllTopics
and its/topics
RequestMapping. - Return a hardcoded list of
Topic
objects which Spring MVC converts to JSON.
- Add TopicService class.
- Inject or Autowire TopicService dependency into TopicController.
- Refactor and move hardcoded list to TopicService.
- Add
getAllTopics
service function.
- Add
getTopic
service function. - Add
/topics/{id}
RequestMapping which calls service function with the PathVariable and returns the output.
- Add POST RequestMethod mapping for
/topics
. - Pass the
Topic
instance from the RequestBody to the TopicService. - Add
addTopic
service function.
- Add PUT RequestMethod mapping for
/topics/{id}
. - Pass the RequestBody
Topic
instance andid
PathVariable to TopicService. - Add
updateTopic
service function. - Add DELETE RequestMethod mapping for
/topics/{id}
. - Pass the
id
PathVariable to TopicService. - Add
deleteTopic
service function.
This is a good time to create an application.properties
file and make any necessary configurations, such as changing the application port. It is good practice to frequently verify the state of the application and test endpoint changes using Postman.
- Add
spring-boot-starter-data-jpa
andderby
embedded database dependencies topom.xml
. - Add Entity annotation to
Topic
class. - Add
Id
primary key annotation. - Add TopicRepository interface extending CrudRepository.
- Autowire TopicRepository to TopicService.
- Refactor TopicService to use built-in TopicRepository CRUD operation methods.
- Set up a Course package in the same way as the Topic package.
- Establish a ManyToOne relationship from
Course
toTopic
. - Add
findByTopicId
function declaration to CourseRepository interface. - Refactor CourseService to use functions provided by CourseRepository.
- Refactor CourseController.
Course API
--------------
GET /topics/{topicId}/courses Get all courses
GET /topics/{topicId}/courses/{id} Get one course
POST /topics/{topicId}/courses Add new course
PUT /topics/{topicId}/courses/{id} Update a course
DELETE /topics/{topicId}/courses/{id} Delete a course
Add the spring-boot-starter-actuator
and package the project as a jar or a traditional war. All good to go!
Open to any suggestions on how to make this better! Much appreciated!