Awesome Open Source
Awesome Open Source

Build

Starter project to create a simple RESTful web service in Kotlin

Updated for Kotlin 1.4.30 and Ktor 1.5.0

Companion article: https://ryanharrison.co.uk/2018/04/14/kotlin-ktor-exposed-starter.html

Getting Started

  1. Clone the repo.
  2. In the root directory execute ./gradlew run
  3. By default the server will start on port 8080. See below Routes section for more information.

Libraries used:

The starter project creates a new in-memory H2 database with one table for Widget instances.

As ktor is async and based on coroutines, standard blocking JDBC may cause performance issues when used directly on the main thread pool (as threads must be reused for other requests). Therefore, another dedicated thread pool is created for all database queries, alongside connection pooling with HikariCP.

Routes:

GET /widget --> get all widgets in the database

GET /widget/{id} --> get one widget instance by id (integer)

POST /widget --> add a new widget to the database by providing a JSON object (converted to a NewWidget instance). e.g -

{
    "name": "new widget",
    "quantity": 64
}

returns

{
    "id": 4,
    "name": "new widget",
    "quantity": 64,
    "dateCreated": 1519926898
}

PUT /widget --> update an existing widgets name or quantity. Pass in the id in the JSON request to determine which record to update

DELETE /widget/{id} --> delete the widget with the specified id

Notifications (WebSocket)

All updates (creates, updates and deletes) to Widget instances are served as notifications through a WebSocket endpoint:

WS /updates --> returns Notification instances containing the change type, id and entity (if applicable) e.g:

{ 
    "type": "CREATE", 
    "id": 12, 
    "entity": { 
      "id": 12, 
      "name": "widget1", 
      "quantity": 5, 
      "dateUpdated": 1533583858169 
    }
}

Testing

The sample Widget service and corresponding endpoints are also tested with 100% coverage. Upon startup of the main JUnit suite (via the test source folder), the server is started ready for testing and is torn down after all tests are run.

  • Unit testing of services with AssertJ - DAO and business logic
  • Integration testing of endpoints using running server with Rest Assured - routing tests/status codes/response structure

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
kotlin (3,364
rest-api (528
websocket (435
netty (147
kotlin-coroutines (59
jackson (36
ktor (22

Find Open Source By Browsing 7,000 Topics Across 59 Categories