2017-10-09 10 views
1

私は次のようにアッカのhttpクエリのparamをアンマーシャリングしたいと思います:akka http - LocalDateとしてクエリパラメータをアンマーシャリングする方法は?

name=jim&age=30&dob=11-20-1990 

import java.time.LocalDate 
import akka.http.scaladsl.marshalling.ToResponseMarshallable 
import akka.http.scaladsl.server._ 
import akka.http.scaladsl.model.StatusCodes 
import akka.http.scaladsl.model.headers.RawHeader 
import akka.http.scaladsl.server.Directives.{complete, _} 
... 
parameters(('name.as[String].?, 'age.as[Int].?, 'dob.as[String].?)) { (name, age, dob) => 

私は、文字列またはintとしてOKすべてをアンマーシャリングすることができますが、私はjava.utilのようDOBをunmarsallしたいと思います。

parameters(('name.as[String].?, 'age.as[Int].?, 'dob.as[LocalDate].?)) { (name, age, dob) => 

しかし、私は次のエラーを取得しています:

[error] routes/Router.scala:141: type mismatch; 
[error] found : (akka.http.scaladsl.common.NameOptionReceptacle[Int], akka.http.scaladsl.common.NameOptionReceptacle[Int], akka.http.scaladsl.common.NameOptionReceptacle[java.time.LocalDate]) 
[error] required: akka.http.scaladsl.server.directives.ParameterDirectives.ParamMagnet 
[error]    parameters(('$top.as[Int].?, '$skip.as[Int].?, 'modified_date.as[LocalDate].?)) { (top, skip, modifiedDate) => 

私はスコープでカスタムLOCALDATEのアンマーシャラーを追加しようとしたが、私はまだのgettinよ、このようなLOCALDATE、グラム同じエラー:

implicit val LocalDateUnmarshaller = new JsonReader[LocalDate] { 

     def read(value: JsValue) = value match { 
     case JsString(x) => LocalDate.parse(x) 
     case x => throw new RuntimeException(s"Unexpected type %s on parsing of LocalDate type".format(x.getClass.getName)) 
     } 
    } 
+0

おかげで、これは、重複しました – Rory

答えて

0

はこれを試してみてください:私が探していたとき、私はその質問を見つけることができませんでしたが

object LocalDateUnmarshaller { 

    implicit val booleanFromStringUnmarshaller: Unmarshaller[String, LocalDate] = 
    Unmarshaller.strict[String, LocalDate] { string ⇒ 
     import java.time.LocalDate 
     import java.time.format.DateTimeFormatter 
     var formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd") 
     val date = LocalDate.parse(string, formatter) 
     date 
    } 

} 
関連する問題