2

我有一个案例类

final case class MyClass(id: Long, eventData: EventsDTO)

final case class EventsDTO(
    customerId: Long,
    eventName: String,
    processTime: DateTime //JodaTime
)

我有自定义编码器和解码器

val dateFormatter = DateTimeFormat.forPattern("yyyyMMdd")
    implicit def TimestampFormat: Encoder[DateTime] with Decoder[DateTime] =
      new Encoder[DateTime] with Decoder[DateTime] {
        override def apply(a: DateTime): Json = Encoder.encodeString.apply(a.toString)

        override def apply(c: HCursor): Result[DateTime] =
          Decoder.decodeString.map(s => DateTime.parse(s, dateFormatter)).apply(c)
      }

我的貘端点为

 val inclusive: Route[Unit, MyClass] =
      endpoint.post
        .in(Paths.api)
        .out(jsonBody[WidgetsResponse])

但是如果运行上面的代码,我会得到

Error:(71, 15) Cannot find a codec for type: com.Model.MyClass, formatted as: sttp.tapir.CodecFormat.Json.
Did you define a codec for: com.Model.MyClass?
Did you import the codecs for: sttp.tapir.CodecFormat.Json?
Is there an implicit schema for: com.Model.MyClass, and all of its components?
(codecs are looked up as implicit values of type Codec[com.Model.MyClass, sttp.tapir.CodecFormat.Json, _];
schemas are looked up as implicit values of type Schema[com.Model.MyClass])
      jsonBody[MyClass]


Error:(71, 15) not enough arguments for method jsonBody: (implicit codec: sttp.tapir.CodecForOptional[com.Model.MyClass, sttp.tapir.CodecFormat.Json, _])sttp.tapir.EndpointIO.Body[com.Model.MyClass, sttp.tapir.CodecFormat.Json, _].
Unspecified value parameter codec.
      jsonBody[MyClass]

有人可以帮我做错什么吗?

processTime: DateTime //JodaTime如果我从中删除EventsDTO,应用程序运行得很好。

4

1 回答 1

1

这是丢失的部分

  implicit val schemaForDateTime: Schema[DateTime] = Schema(SString)
于 2020-06-04T21:50:34.867 回答