Scala Akka HTTP casting parameter as java.time.ZonedDateTime

前端 未结 1 1858
忘了有多久
忘了有多久 2021-01-14 12:31

I\'m working on a REST service using Akka HTTP (in Scala). I would like a parameter that is passed in to a http get request to be converted to the ZonedDateTime type. The co

1条回答
  •  迷失自我
    2021-01-14 13:01

    As ZonedDateTime is not natively unmarshalled by Akka-HTTP, you will need to provide a custom unmarshaller to the parameters directive.

    This functionality is briefly described in the docs here.

    Your unmarshaller can be created from a function by using Unmarshaller.strict, e.g.

      val stringToZonedDateTime = Unmarshaller.strict[String, ZonedDateTime](ZonedDateTime.parse)
    

    This example assumes your param is provided in an ISO format. If it's not, you'll need to amend the unmarshalling function.

    You can then use the unmarshaller passing it to the parameters directive:

    parameters("testparam".as(stringToZonedDateTime)){
                (testparam) =>
                  complete(testparam.toString)
              }
    

    0 讨论(0)
提交回复
热议问题