https://kotlinlang.org logo
#http4k
Title
# http4k
j

joscha.alisch

04/10/2018, 5:50 PM
Thanks @dave! It’s as simple as this:
Copy code
import org.http4k.contract.*
import org.http4k.core.*
import org.http4k.format.Jackson
import org.http4k.format.Jackson.auto
import org.http4k.routing.bind
import org.http4k.routing.routes
import org.http4k.server.Jetty
import org.http4k.server.asServer

data class MyDataClass(val something: String)
val requestBody = Body.auto<MyDataClass>().toLens()

fun main(args: Array<String>) {
    routes(
        "/api" bind contract(OpenApi(ApiInfo("My great API", "v1.0"), Jackson),"/route" meta {
            body = requestBody
        } bindContract Method.GET to ::handler)
    ).asServer(Jetty(5000)).start()
}

fun handler(request: Request): Response {
    return Response(Status.OK)
}
gives me
Copy code
{
  “swagger” : “2.0",
  “info” : {
    “title” : “My great API”,
    “version” : “v1.0”,
    “description” : “”
  },
  “basePath” : “/”,
  “tags” : [ ],
  “paths” : {
    “/api/route” : {
      “get” : {
        “tags” : [ “/api” ],
        “summary” : “<unknown>“,
        “description” : null,
        “produces” : [ ],
        “consumes” : [ “application/json” ],
        “parameters” : [ {
          “in” : “body”,
          “name” : “body”,
          “description” : null,
          “required” : true,
          “type” : “object”
        } ],
        “responses” : { },
        “supportedContentTypes” : [ ],
        “security” : [ ]
      }
    }
  },
  “securityDefinitions” : { },
  “definitions” : { }
}
d

dave

04/10/2018, 6:17 PM
Yeah - you'll need to give http4k an example of your body to get this to work.
There should be an example of how to set the example request in the docs.
It can't infer to structure without an example
(ie from just the lens)
j

joscha.alisch

04/10/2018, 6:19 PM
Ahh got it, so I need this for example:
receiving(requestBody to MyDataClass("example"))
Couldn’t find an example for that in the docs though
Anyways, thanks a lot - it works now 🙂
d

dave

04/10/2018, 6:35 PM
I'll check the docs for an example. Thanks 😀
👍 1
2 Views