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

elifarley

03/06/2018, 9:46 PM
I have a
Copy code
class SimpleObject(val id: String, val attrs: Map<String, Any?>,
                        val created: ZonedDateTime,
                        val updated: ZonedDateTime
)
If I use
with( Header.zonedDateTime().required("last-modified") of obj.created)
, I end up with multiple attributes like
"created":{"dateTime":{"date":{"year":2018,"month":3,...
). I think it should be a single String attribute, based on my understanding of
this.map(ZonedDateTime::parse, DateTimeFormatter.ISO_ZONED_DATE_TIME::format)
in file lensSpec.kt
What am I doing wrong?
s

s4nchez

03/06/2018, 9:49 PM
@elifarley the lenSpec mapping you're referring to is used in other lenses (query/header etc). The Body.auto relies on the underlying
Json
implementation
e

elifarley

03/06/2018, 9:50 PM
Sorry, I pasted the wrong part of the code
I'll edit my post
s

s4nchez

03/06/2018, 9:50 PM
Which http4k json library are you using?
e

elifarley

03/06/2018, 9:51 PM
Pronto, editei lá.
🇧🇷 1
🙂 1
Gson
I'm using Header.zonedDateTime
s

s4nchez

03/06/2018, 9:58 PM
I've tried
Copy code
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.with
import org.http4k.lens.Header
import org.http4k.lens.zonedDateTime
import java.time.ZonedDateTime

class SimpleObject(val id: String, val attrs: Map<String, Any?>,
                   val created: ZonedDateTime,
                   val updated: ZonedDateTime)

fun main(args: Array<String>) {
    val obj = SimpleObject("foo", emptyMap(), ZonedDateTime.now(), ZonedDateTime.now())
    val request = Request(Method.GET, "/")
        .with(Header.zonedDateTime().required("last-modified") of obj.created)
    println(request)
}
and got
Copy code
request = GET / HTTP/1.1
last-modified: 2018-03-06T21:56:51.882Z[Europe/London]
e

elifarley

03/06/2018, 10:00 PM
Here's my function:
Copy code
customerService[id]?.let { obj ->

                Response(OK).with(
                        bodySimpleObject of obj
                ).with(headerLastModified of obj.updated)

            } ?: Response(Status.NOT_FOUND)
val headerLastModified = Header.zonedDateTime().required("last-modified")
s

s4nchez

03/06/2018, 10:05 PM
That generates a message like
Copy code
HTTP/1.1 200 OK
content-type: application/json; charset=utf-8
last-modified: 2018-03-06T22:05:09.644Z[Europe/London]

{"id":"foo","attrs":{},"created":{"dateTime":{"date":{"year":2018,"month":3,"day":6},"time":{"hour":22,"minute":5,"second":9,"nano":644000000}},"offset":{"totalSeconds":0},"zone":{"id":"Europe/London"}},"updated":{"dateTime":{"date":{"year":2018,"month":3,"day":6},"time":{"hour":22,"minute":5,"second":9,"nano":717000000}},"offset":{"totalSeconds":0},"zone":{"id":"Europe/London"}}}
e

elifarley

03/06/2018, 10:06 PM
indeed. How can I get
2018-03-06T21:56:51.882Z[Europe/London]
?
s

s4nchez

03/06/2018, 10:06 PM
The json serialisation is dependent on how the json library is configure to handle
ZonedDateTimes
. We're using a very vanilla config
e

elifarley

03/06/2018, 10:06 PM
but why your first example worked ?
It looks as if it works if you use it in a Request intance, while it won't work if used in a Response instance.
s

s4nchez

03/06/2018, 10:08 PM
It works for headers because we configure the serialisation in the lensSpec
It won't necessarily work for json because it depends on how the json library is configured
And the behaviour is the same for both request and response in this case.
e

elifarley

03/06/2018, 10:10 PM
Hmm.. But I think I'm using it on the header, not with json, so it should work... ?
oh...I see
Duh! the header is working just fine!
I was looking at the bloody BODY but didn't notice it, sorry ! 😁
thanks for your quick replies 🙂
s

s4nchez

03/06/2018, 10:15 PM
Regarding the body serialisation, if you want to generate the dates in JSON in the same way as the header, you need to customise the Gson adapter. I've put together an example here: https://gist.github.com/s4nchez/9a7e65630c8a71cf6a72b0691fd80a0f
👍 1
e

elifarley

03/06/2018, 10:17 PM
Hey, that's really great, thank you!!
s

s4nchez

03/06/2018, 10:19 PM
You're welcome 🙂
e

elifarley

03/06/2018, 10:20 PM
Hmmm.. how do I link it so that it'll be called by Body.auto ?
import myapp.MyCustomGson.auto ?
👍 2
good
It worked beautifully, thanks 🙂
😀 1
12 Views