简体   繁体   English

找不到参数 um 的隐式值

[英]Could not find implicit value for parameter um

I had an Main app with the following structure:我有一个具有以下结构的主应用程序:

implicit val orgs: RootJsonFormat[GHOrg]                = jsonFormat3(GHOrg)
implicit val users: RootJsonFormat[GHUser]              = jsonFormat2(GHUser)
implicit val repos: RootJsonFormat[GHRepo]              = jsonFormat3(GHRepo)
case class GHUser(login: String, contributions: Option[Int] = None)
case class GHRepo(name: String, owner: GHUser, contributors_url: String)
case class GHOrg(name: String, repos_url: String, members_url: String)

At some point I do在某些时候我会

Unmarshal(e).to[List[GHRepo]]

And everything works, however, I have been doing a little bit of code cleaning and I have moved the three case classes above to a different package, and now I am getting title's error.一切正常,但是,我一直在进行一些代码清理,并将上面的三个案例类移动到不同的 package,现在我得到了标题的错误。 I have tried this suggestion ( spray-json error: could not find implicit value for parameter um ) but does not work.我已经尝试过这个建议( spray-json 错误:找不到参数 um 的隐含值)但不起作用。

Here is how the current code (Not compiling):这是当前代码的方式(未编译):

import akka.actor.ActorSystem
import akka.http.javadsl.model.StatusCodes
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import com.elbauldelprogramador.domain.{GHOrg, GHRepo, GHUser}
import org.slf4j.LoggerFactory
import spray.json.DefaultJsonProtocol._
import spray.json.RootJsonFormat

object Main extends App {

  implicit val system: ActorSystem                        = ActorSystem()
  implicit val materializer: ActorMaterializer            = ActorMaterializer()
  implicit val executionContext: ExecutionContextExecutor = system.dispatcher
  implicit val orgs: RootJsonFormat[GHOrg]                = jsonFormat3(GHOrg)
  implicit val users: RootJsonFormat[GHUser]              = jsonFormat2(GHUser)
  implicit val repos: RootJsonFormat[GHRepo]              = jsonFormat3(GHRepo)
// ....
Unmarshal(e).to[List[GHRepo]] // Error
// ....
}

Here is the full error:这是完整的错误:

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.Unmarshaller[akka.http.scaladsl.model.ResponseEntity,List[com.elbauldelprogramador.domain.GHUser]] [error] val users = Unmarshal(e).to[List[GHUser]] [error] ^ could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.Unmarshaller[akka.http.scaladsl.model.ResponseEntity,List[com.elbauldelprogramador.domain.GHUser]] [error] val users = Unmarshal(e) .to[List[GHUser]] [错误] ^

I finally was able to solve it reading spray's docs :我终于能够阅读喷雾的文档来解决它:

Basically, I created a separated file for each case class and one object called MyJsonProtocol with the following code:基本上,我为每个案例 class 和一个名为MyJsonProtocol的 object 创建了一个单独的文件,代码如下:

object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val users = jsonFormat2(GHUser)
  // ...
}

Then in my main app I just import both things:然后在我的主应用程序中,我只导入这两个东西:

import com.elbauldelprogramador.api.MyJsonProtocol._
import com.elbauldelprogramador.domain.{GHOrg, GHRepo, GHUser}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM