首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在scala的喷雾中,从普通的解编组器中获得一个FromRequestUnmarshaller

在scala的喷雾中,从普通的解编组器中获得一个FromRequestUnmarshaller
EN

Stack Overflow用户
提问于 2014-06-25 23:31:12
回答 1查看 1.6K关注 0票数 2

在scala喷雾中,是否有一种将Unmarshaller[T]转换为FromRequestUnmarshaller[T]的方法。我被困在试图使entity指令工作,而不使用implicits。例如:

代码语言:javascript
复制
...
} ~ post {
  path("myPath") {
    entity(sprayJsonUnmarshaller[MyCaseClass](myCaseClassRootJsonFormat)) { myCaseClass =>
      complete { handle(myCaseClass) }
    }
  } ~ ...

编译器错误:

代码语言:javascript
复制
Multiple markers at this line
    - type mismatch; found : spray.httpx.unmarshalling.Unmarshaller[MyCaseClass] (which 
     expands to) spray.httpx.unmarshalling.Deserializer[spray.http.HttpEntity,MyCaseClass] 
     required: spray.httpx.unmarshalling.FromRequestUnmarshaller[?] (which expands to) 
     spray.httpx.unmarshalling.Deserializer[spray.http.HttpRequest,?]
    - type mismatch; found : spray.httpx.unmarshalling.Unmarshaller[MyCaseClass] (which 
     expands to) spray.httpx.unmarshalling.Deserializer[spray.http.HttpEntity,MyCaseClass] 
     required: spray.httpx.unmarshalling.FromRequestUnmarshaller[?] (which expands to) 
     spray.httpx.unmarshalling.Deserializer[spray.http.HttpRequest,?]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-26 06:47:36

在这部分,喷雾在很大程度上取决于隐式分辨率。我可能是不正确的,但正如我所知,现在有简单而优雅的方法来做到这一点。就像它的设计一样,您应该执行以下指令:entity(as[MyCaseClass])。然后,如果您看一看as[_]指令,就会发现一个简单的解组器(它接受一个实体并使您的case类)到fromRequestUnmarshaller (从HttpRequest ->案例类),所有隐式扩展程序都可以找到here。因此,当您调用entity(as[MyCaseClass])时,它会扩展到以下内容:

代码语言:javascript
复制
entity {
  as[MyCaseClass] {
    fromRequestUnmarshaller[MyCaseClass] {
      fromMessageUnmarshaller[MyCaseClass] {
        sprayJsonUnmarshaller[MyCaseClass](myCaseClassRootJsonFormat)
      }
    }
  }
}

如果您想要使它显式,那么您应该用上面的形式写它。这样您就可以丢弃as[MyCaseClass]

另一方面,您可以选择另一个显式的方法-提取实体并将其转换为json:

代码语言:javascript
复制
requestInstance { req =>
  val json = req.entity.asString.parseJson
  json.convertTo(myCaseClassRootJsonFormat)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24420249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档