Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marx-wrld/kotlinwithspringboot
Kotlin with Spring Boot, web application for starters.
https://github.com/marx-wrld/kotlinwithspringboot
kotlin rest-api spring-boot
Last synced: 2 days ago
JSON representation
Kotlin with Spring Boot, web application for starters.
- Host: GitHub
- URL: https://github.com/marx-wrld/kotlinwithspringboot
- Owner: Marx-wrld
- Created: 2023-12-08T16:55:10.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-02-04T17:28:17.000Z (10 months ago)
- Last Synced: 2024-02-05T14:46:27.547Z (9 months ago)
- Topics: kotlin, rest-api, spring-boot
- Language: Kotlin
- Homepage:
- Size: 83 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# KotlinWithSpringboot
Kotlin with Spring Boot, web application for starters.#### 1. Setting Up a Kotlin-Spring Boot Project
You can use Spring Initializer to set up a new project. Visit https://start.spring.io/ and fill in the necessary details:
- Code editor: IntelliJ IDEA
- Project: Gradle Project or Maven Project
- Language: Kotlin
- Spring Boot: Latest stable version
- Project Metadata: Group, Artifact, Name, Description, etc.Click "Generate" to download the project zip file.
#### 2. Extract and Open the Project
Extract the downloaded zip file and open the project in your preferred IDE. If you're using IntelliJ IDEA, it has excellent support for Kotlin.
#### 3. Create a Kotlin Data Class
Create a data class to represent your model. In `src/main/kotlin/com/example/demo/model`, create a file named `Book.kt`:
```
package com.example.demo.modeldata class Book(val id: Long, val title: String, val author: String)
```
#### 4. Create a ControllerCreate a controller to handle HTTP requests. In `src/main/kotlin/com/example/demo/controller`, create a file named `BookController.kt`
```
package com.example.demo.controllerimport com.example.demo.model.Book
import org.springframework.web.bind.annotation.*@RestController
@RequestMapping("/api/books")
class BookController {private val books = mutableListOf()
@GetMapping
fun getAllBooks(): List {
return books
}@GetMapping("/{id}")
fun getBookById(@PathVariable id: Long): Book? {
return books.find { it.id == id }
}@PostMapping
fun addBook(@RequestBody book: Book): Book {
books.add(book)
return book
}@PutMapping("/{id}")
fun updateBook(@PathVariable id: Long, @RequestBody updatedBook: Book): Book? {
val index = books.indexOfFirst { it.id == id }
if (index != -1) {
books[index] = updatedBook
return updatedBook
}
return null
}@DeleteMapping("/{id}")
fun deleteBook(@PathVariable id: Long): Boolean {
return books.removeIf { it.id == id }
}
}
```
#### 5. Run the ApplicationRun your Spring Boot application. If you're using IntelliJ IDEA, you can run the DemoApplication class.
#### 6. Test the APIs
You can test your API using tools like Insomnia, Postman, or a web browser.
- To get all books: `GET http://localhost:8080/api/books`
- To add a book: `POST http://localhost:8080/api/books` with a JSON payload.
- To update a book: `PUT http://localhost:8080/api/books/{id}` with a JSON payload.
- To delete a book: `DELETE http://localhost:8080/api/books/{id}`