加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 带有Http4s的Circe编码器和解码器

发布时间:2020-12-16 18:09:15 所属栏目:安全 来源:网络整理
导读:我正在尝试使用http4s,circe和http4s-circe. 下面我试图使用circe的自动派生功能. import org.http4s.client.blaze.SimpleHttp1Clientimport org.http4s.Status.ResponseClass.Successfulimport io.circe.syntax._import org.http4s._import org.http4s.head
我正在尝试使用http4s,circe和http4s-circe.

下面我试图使用circe的自动派生功能.

import org.http4s.client.blaze.SimpleHttp1Client
import org.http4s.Status.ResponseClass.Successful
import io.circe.syntax._
import org.http4s._
import org.http4s.headers._
import org.http4s.circe._
import scalaz.concurrent.Task
import io.circe._

final case class Login(username: String,password: String)
final case class Token(token: String)

object JsonHelpers {
   import io.circe.generic.auto._
   implicit val loginEntityEncoder : EntityEncoder[Login] = jsonEncoderOf[Login]
   implicit val loginEntityDecoder : EntityDecoder[Login] = jsonOf[Login]
   implicit val tokenEntityEncoder: EntityEncoder[Token] = jsonEncoderOf[Token]
   implicit val tokenEntityDecoder : EntityDecoder[Token] = jsonOf[Token]
}

object Http4sTest2 extends App {
   import JsonHelpers._
   val url = "http://"
   val uri = Uri.fromString(url).valueOr(throw _)
   val list = List[Header](`Content-Type`(MediaType.`application/json`),`Accept`(MediaType.`application/json`))
   val request = Request(uri = uri,method = Method.POST)
      .withBody(Login("foo","bar").asJson)
      .map{r => r.replaceAllHeaders(list :_*)}.run
   val client = SimpleHttp1Client()
   val result = client.fetch[Option[Token]](request){
      case Successful(response) => response.as[Token].map(Some(_))
      case _ => Task(Option.empty[Token])
   }.run
   println(result)
}

我得到了这两个编译器错误的多个实例

Error:scalac: missing or invalid dependency detected while loading class file 'GenericInstances.class'.
Could not access type Secondary in object io.circe.Encoder,because it (or its dependencies) are missing. Check your build definition for
missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
A full rebuild may help if 'GenericInstances.class' was compiled against an incompatible version of io.circe.Encoder.


Error:(25,74) could not find implicit value for parameter encoder: io.circe.Encoder[Login]
   implicit val loginEntityEncoder : EntityEncoder[Login] = jsonEncoderOf[Login]

解决方法

我能够解决这个问题.我在google上搜索了sbt circe依赖项,我复制粘贴了第一个搜索结果.这是0.1左右,这就是为什么事情对我不起作用.

我将依赖项更改为

libraryDependencies ++= Seq(
  "org.http4s" %% "http4s-core" % http4sVersion,"org.http4s" %% "http4s-dsl" % http4sVersion,"org.http4s" %% "http4s-blaze-client" % http4sVersion,"org.http4s" %% "http4s-circe" % http4sVersion,"io.circe" %% "circe-core" % "0.7.0","io.circe" %% "circe-generic" % "0.7.0"
)

现在自动派生工作正常,我能够编译下面的代码

import org.http4s.client.blaze.SimpleHttp1Client
import org.http4s._
import org.http4s.headers._
import org.http4s.circe._

import scalaz.concurrent.Task
import io.circe.syntax._
import io.circe.generic.auto._
import org.http4s.Status.ResponseClass.Successful

case class Login(username: String,password: String)
case class Token(token: String)

object JsonHelpers {
   implicit val loginEntityEncoder : EntityEncoder[Login] = jsonEncoderOf[Login]
   implicit val loginEntityDecoder : EntityDecoder[Login] = jsonOf[Login]
   implicit val tokenEntityEncoder: EntityEncoder[Token] = jsonEncoderOf[Token]
   implicit val tokenEntityDecoder : EntityDecoder[Token] = jsonOf[Token]
}

object Http4sTest2 extends App {
   import JsonHelpers._
   val url = "http://"
   val uri = Uri.fromString(url).valueOr(throw _)
   val list = List[Header](`Content-Type`(MediaType.`application/json`),"bar").asJson)
      .map{r => r.replaceAllHeaders(list :_*)}.run
   val client = SimpleHttp1Client()
   val result = client.fetch[Option[Token]](request){
      case Successful(response) => response.as[Token].map(Some(_))
      case _ => Task(Option.empty[Token])
   }.run
   println(result)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读