Generating data classes for messages

On the web...

This Heroku app demonstrates how to use JSON and XML auto-marshalling to communicate using typesafe Body lenses.

Or manually using a filter...

This example show the usage of the GenerateDataClasses to generate Kotlin data class code for JSON messages from a remote endpoint. When used in conjunction with the "auto body-marshalling" functionality available with JSON libraries such as Jackson and GSON, this provides a super-fast way to integrate with upstream remote APIs in a typesafe way.

Gradle setup

implementation group: "org.http4k", name: "http4k-core", version: "3.269.0"
implementation group: "org.http4k", name: "http4k-format-gson", version: "3.269.0"

Code

package cookbook.generating_data_classes

import org.http4k.client.ApacheClient
import org.http4k.core.Method.GET
import org.http4k.core.Request
import org.http4k.core.then
import org.http4k.filter.GenerateDataClasses
import org.http4k.format.Jackson

fun main() {

    val request = Request(GET, "http://api.icndb.com/jokes/random/3")

    GenerateDataClasses(Jackson, System.out).then(ApacheClient()).invoke(request)
}