Build RESTFUL APIs using Kotlin and Spring Boot

Why take this course?
¡Hola! It seems like you're looking to build a DELETE endpoint in a course catalog service using Kotlin and Spring Boot, and you're following a structured curriculum that covers various aspects of the development process. Let's focus on creating the DELETE endpoint for deleting a course.
Here's a step-by-step guide to create the DELETE endpoint in your CourseController
using Kotlin:
Step 1: Define the Endpoint in CourseController
In your CourseController
class, you will need to add a function that handles HTTP DELETE requests. Spring Boot already provides an @DeleteMapping
annotation for this purpose.
@RestController
@RequestMapping("/api/courses")
class CourseController(
private val courseService: CourseService
) {
// ... other endpoints like GET, POST, PUT
@DeleteMapping("/{id}")
fun deleteCourse(@PathVariable id: Long): ResponseEntity<*> {
return ResponseEntity.ok().body(courseService.deleteCourse(id))
}
}
In the above code snippet, @DeleteMapping("/{id}")
specifies that the deleteCourse
function should handle DELETE requests targeting /api/courses/{id}
. The {id}
is a path variable that will extract the course ID from the request URL.
Step 2: Implement the Business Logic in CourseService
Your CourseService
should contain the logic to delete a course by its ID, possibly interacting with the database. Here's an example of how you might implement this:
@Service
class CourseService(
private val courseRepository: CourseRepository
) {
fun deleteCourse(id: Long): ResponseEntity<Boolean> {
return courseRepository.deleteById(id) run {
if (this) ResponseEntity.ok(true) else ResponseEntity.notFound().build()
}
}
}
In this example, CourseRepository
is an interface that extends JpaRepository<Course, Long>
, which provides the deleteById
method to delete a course by its ID.
Step 3: Write Unit Tests for the DELETE Endpoint
To test the DELETE endpoint, you'll write a unit test that simulates a DELETE request and verifies the response.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class CourseControllerTests {
// ... other endpoint tests
@Test
fun `deleteCourse should return no content when course is found and deleted`() {
val courseIdToDelete = 1L
val course = Course(title = "Test Course", description = "Description")
courseRepositoryInContext.save(course)
mockMvc.delete("/api/courses/${courseIdToDelete}")
.andExpect(status().isNoContent())
}
@Test
fun `deleteCourse should return not found when course is not found`() {
val nonExistentCourseId = 999L
mockMvc.delete("/api/courses/${nonExistentCourseId}")
.andExpect(status().isNotFound())
}
}
In these tests, mockMvc
is an instance of MockMvc
that allows you to perform HTTP requests against your controller. The courseRepositoryInContext
is a mock of your repository that's injected into the test context.
Step 4: Write Integration Tests for the DELETE Endpoint
Integration tests are more realistic as they use the application context and database.
@DataJpaTest
class CourseRepositoryTests {
// ... other repository tests
@Test
fun `deleteCourse should return true when course is deleted`() {
val course = Course(title = "Test Course", description = "Description")
courseRepository.save(course)
assertTrue(courseRepository.deleteById(course.id).isPresent)
}
}
In this integration test, you're directly using the CourseRepository
to delete a course and asserting that it was successfully deleted (i.e., isPresent
should return true
).
Step 5: Run Your Application
After implementing the endpoint and writing your tests, you can run your Spring Boot application to ensure everything works as expected. You can use an API client like Postman or curl to manually test the DELETE endpoint by sending a request to http://localhost:8080/api/courses/{id}
.
Remember to handle exceptions and errors gracefully in your actual code, and consider the security implications of deleting resources (e.g., authentication and authorization checks).
Good luck with your course catalog service, and I hope this guide helps you implement the DELETE endpoint!
Loading charts...
Comidoc Review
Our Verdict
Though the course content is well-structured and covers a wide range of topics related to Kotlin and Spring Boot, the learning experience might be dampened by the instructor's accent, fast pacing during coding demonstrations, and outdated references. Attempting this course requires patience due to copy & pasted test cases and an assumption that students can follow along despite changes in the IntelliJ interface. However, it provides a solid foundation for developing applications using Kotlin and Spring Boot.
What We Liked
- Covers a wide range of topics from Kotlin basics to building Restful APIs using Spring Boot and testing with JUnit5
- In-depth explanations of Java and Kotlin interoperability
- Instructor is knowledgeable about the subject matter
- Clear explanations of the development process, including dev and testing (unit and IT tests)
Potential Drawbacks
- Strong accent and pronunciation make it challenging to understand key concepts even with subtitles
- Instructor speaks too fast while pasting text and code, making it difficult to follow and code along
- Test cases are mostly copy & pasting, leading to easy misses and potential errors
- Outdated references for PostgresDB, DockerSetup, and TestContainers instructions