scala中的通用返回类型与akka编组

时间:2017-11-16 08:00:10

标签: json scala generics akka spray

我有以下相当简单的代码:

  renderer.setView(gameCam.combined, (gameCam.position.x*0.5 - gameCam.viewportWidth / 2 - 2), gameCam.position.y - gameCam.viewportHeight / 2, gameCam.viewportWidth, gameCam.viewportHeight);

JsonSupport包含json格式定义,用于将传入的json编组到响应案例类。只要我没有尝试为sendRequest方法定义泛型版本就可以正常工作,该方法可以返回InverterResponse或MeterResponse类型。编译很好,但我得到了:

  renderer.setView(gameCam.combined, (gameCam.position.x - gameCam.viewportWidth / 2 - 2)*0.5, gameCam.position.y - gameCam.viewportHeight / 2, gameCam.viewportWidth, gameCam.viewportHeight);

大约两年没有使用scala ...所以我的知识可能会有点生锈...

1 个答案:

答案 0 :(得分:1)

通过挖掘源代码和文档来找到答案。

https://doc.akka.io/docs/akka-http/current/scala/http/common/unmarshalling.html

https://github.com/akka/akka-http/blob/v10.0.3/akka-http/src/main/scala/akka/http/scaladsl/unmarshalling/Unmarshal.scala

Umarshall需要一个隐含的Unmarshaller。在方法签名中提供它就像解决问题一样:

private def sendRequest [T](inverterRealtimeURLPath:String)(隐式m:Unmarshaller [ResponseEntity,T]):Future [T] = {