从sbt运行TwitterServer时出现此错误:
SEVERE: LoadService: failed to
instantiate 'com.twitter.finagle.stats.MetricsExporter'
for the requested service 'com.twitter.finagle.http.HttpMuxHandler'
com.fasterxml.jackson.databind.JsonMappingException:
Incompatible Jackson version: 2.7.1
Run Code Online (Sandbox Code Playgroud)
2.7杰克逊的依赖从其他地方(circe)传递.我以为我可以用这种方式将它覆盖到2.6.7,但似乎没有效果:
val jacksonV = "2.6.7"
val `jackson-core` = "com.fasterxml.jackson.core" % "jackson-core" % jacksonV
val `jackson-databind` = "com.fasterxml.jackson.core" % "jackson-databind" % jacksonV
val `jackson-annotations` = "com.fasterxml.jackson.core" % "jackson-annotations" % jacksonV
val `jackson-datatype-jsr310` = "com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % jacksonV
dependencyOverrides += `jackson-core`
dependencyOverrides += `jackson-databind`
dependencyOverrides += `jackson-annotations`
dependencyOverrides += `jackson-datatype-jsr310`
Run Code Online (Sandbox Code Playgroud)
知道为什么这不会覆盖任何东西吗?
我正在尝试使用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 …Run Code Online (Sandbox Code Playgroud) 我有代码片段
cursor.downField("params").downField("playlist").downField("items").as[List[Clip]]
Run Code Online (Sandbox Code Playgroud)
Clip是一个简单的字符串和数字类.传入的Json应该包含一个json对象"播放列表",其中包含一个"项目"数组,其中每个项目都是一个剪辑.所以json看起来应该是这样的
{
"playlist": {
"name": "Sample Playlist",
"items": [
{
"clipId":"xyz",
"name":"abc"
},
{
"clipId":"pqr",
"name":"def"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码片段,我收到编译错误:
Error:(147, 81) could not find implicit value for parameter d:
io.circe.Decoder[List[com.packagename.model.Clip]]
cursor.downField("params").downField("playlist").downField("items").as[List[Clip]]
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?如何使用circe为简单项目的列表/数组设置解码?
我使用scala 2.11.8与circe 0.7.0
我使用scalajs与API区分null发送的JSON中不存在的字段和字段.
我正在寻找一种编码为JSON的方法,case class包含Option[T]我将设置None为表示缺失值的字段的scala :
case class Foo(
optionalFieldOne: Option[Int] = 42,
optionalFieldTwo: Option[Int] = null,
optionalFieldThree: Option[Int] = None
)
implicit FooEncoder: Encoder[Foo] = deriveEncoder[Foo]
val test = Foo()
//What the code actually produces
test.asJson.noSpace
//>{"optionalFieldOne": 42,"optionalFieldTwo":null, "optionalFieldThree":null}
//What I would like
test.asJson.noSpace
//>{"optionalFieldOne": 42,"optionalFieldTwo":null}
Run Code Online (Sandbox Code Playgroud)
circe是否提供了任何可能的配置?你知道如何访问它,我已经查看了所有发行说明,github问题和他们的网站文档没有成功.
在这种配置选项不可用的情况下,有人会如何正确实现它?
已经为此工作了几天,但仍然不知道发生了什么:得到了 finch Web 服务,build.sbt 依赖项如下所示:
"com.github.finagle" %% "finch-circe" % finchVersion changing(),
"com.github.finagle" %% "finch-core" % finchVersion changing(),
"com.github.finagle" %% "finch-jackson" % finchVersion changing(),
"com.github.finagle" %% "finch-test" % finchVersion changing(),
"com.twitter" %% "twitter-server" % "1.28.0",
Run Code Online (Sandbox Code Playgroud)
Finch 版本是 0.14.0。端点看起来像:
def makeService(statsReceiver: StatsReceiver): Service[Request, Response] = {
//val getUserCounter = statsReceiver.counter("get_user_counter")
(
MyEndpoint.endpoint1()
:+: SomeEndpoint.deleteEntity()
:+: SomeEndpoint.createEntity()
:+: SomeEndpoint.updateEntity()
) handle {
case e: InvalidClientError => Unauthorized(e)
case e: InvalidContextError => BadRequest(e)
case e: RelevanceError => BadRequest(e)
case e: Exception => InternalServerError(e)
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建 Akka Http REST post 端点,将 JSON 对象映射到定义的案例类
import io.circe.Decoder, io.circe.generic.auto._
case class JobEntity(id: Option[Long] = None, name: String, description: String, json_data :java.sql.blob)
Run Code Online (Sandbox Code Playgroud)
JSON 的类型为
{
"id": "124",
"name": "MYJOB",
"description": "Test Job"
}
Run Code Online (Sandbox Code Playgroud)
现在我想将整个 JSON 映射到“json_data”,作为案例类中定义的 blob
post {
entity(as[JobEntity]) { jobEntity: JobEntity =>
complete(createJob(jobEntity).map(_.asJson))
}
}
Run Code Online (Sandbox Code Playgroud)
我知道 .map(_.asJson) 会将 json 映射到 JobEntity,如果不是这样,请纠正
我如何将整个 JSON 映射到 json_data。
我想将 FieldMapping 类的集合保存为 json 字符串 -
abstract class Field {
def clazz: Class[_]
def name: String
}
case class StringField(name: String) extends Field {
override def clazz: Class[_] = classOf[String]
}
case class DateField(name: String) extends Field {
override def clazz: Class[_] = classOf[Date]
}
Run Code Online (Sandbox Code Playgroud)
...等 - 完整代码在这里: https://github.com/alexeyOnGitHub/scala-typesafe/blob/master/src/main/scala/com/example/model/Field.scala
瑟茜代码:
import com.example.model.{DateField, Field, FieldMapping, StringField}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
object CirceBoilerplateForConfigs {
implicit val fieldDecoder: Decoder[StringField] = deriveDecoder[StringField]
implicit val fieldEncoder: Encoder[StringField] = deriveEncoder[StringField]
implicit val dateDecoder: Decoder[DateField] …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我拥有的案例类创建编码器和解码器:
case class Road(id: String, light: RoadLight, names: Map[String, String])
Run Code Online (Sandbox Code Playgroud)
RoadLight 是一个 java 类,带有枚举。
public enum RoadLight {
red,yellow,green
}
Run Code Online (Sandbox Code Playgroud)
我尝试过进行半自动编码和解码:制作隐式编码器和解码器。
我从 Map[String,String] 类型开始:
implicit val namesDecoder: Decoder[Map[String, String]] = deriveDecoder[Map[String, String]]
implicit val namesEncoder: Encoder[Map[String, String]] = deriveEncoder[Map[String, String]]
Run Code Online (Sandbox Code Playgroud)
但我确实得到了他们两个的错误!
1: 找不到 io.circe.generic.decoding.DerivedDecoder[A] 类型的 Lazy 隐式值
2:错误:方法deriveDecoder没有足够的参数:(隐式解码:shapeless.Lazy[io.circe.generic.decoding.DerivedDecoder[A]])io.circe.Decoder[A]。未指定值参数解码。隐式 val 名称解码器:解码器[Map[String,String]]=deriveDecoder
我已经按照书本做了一切,无法理解出了什么问题。我什至没有尝试解析案例类,只是解析地图,甚至这也不起作用。
有任何想法吗?谢谢!
给定以下案例类别LogMessage:
import io.circe.{Decoder, Encoder}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import enumeratum.{CirceEnum, Enum, EnumEntry}
import io.circe.syntax._
sealed trait LogLevel extends EnumEntry
object LogLevel extends Enum[LogLevel] with CirceEnum[LogLevel] {
val values = findValues
case object Warning extends LogLevel
case object Error extends LogLevel
case object Info extends LogLevel
case object Success extends LogLevel
}
object LogMessage {
implicit val logMessageDecoder: Decoder[LogMessage] = deriveDecoder[LogMessage]
implicit val logMessageEncoder: Encoder[LogMessage] = deriveEncoder[LogMessage]
}
case class LogMessage(level: LogLevel, text: String, args: List[String], date: Long)
Run Code Online (Sandbox Code Playgroud)
case class …Run Code Online (Sandbox Code Playgroud) I have a request like the following
val request =
Request[IO](
method = POST,
uri = Uri.uri("..."),
headers = Headers(
Authorization(BasicCredentials("...", "..."))
)
)
.withEntity(PaymentIntentRequest2(2000, "usd"))
Run Code Online (Sandbox Code Playgroud)
我正在查看源代码,看起来它withEntity继承了嵌套的标头EntityDecoder,因此上面的代码默认为Content-Type: application/json. 好像我明确地传递UrlForm一切都很好。
不幸的是,我所点击的 API 预期数据为,x-www-form-urlencoded并且考虑到目标 API 与所有不同端点/请求的复杂性,我希望找到一种将给定案例类编码为表单的方法。最好的方法是什么?
我努力了:
显式指定Content-Type但这不起作用,因为继承类型优先
Product构建从到 的隐式泛型转换UrlForm(目前是扩展方法)
implicit class UrlFormEncode[+B <: Product](val u: B) {
def asUrlForm: UrlForm =
u.productElementNames
.zip(u.productIterator)
.foldLeft(UrlForm()) { (a, b) =>
a.combine(UrlForm(b._1 -> b._2.toString))
}
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是 …