阿卡-HTTP:无法为参数解组
问题描述:
我喷JSON支持发现隐含的价值看起来像这样阿卡-HTTP:无法为参数解组
object MarshallingSupport extends SprayJsonSupport {
implicit def json4sFormats: Formats = DefaultFormats
}
而在我的路线我想请求映射到DTO
object Main extends App with AppConfig with BaseService with MainActorSystem {
val processor = system.actorOf(Props(), "processorActor")
val view = system.actorOf(Props(), "processorActor")
override protected implicit val executor: ExecutionContext = system.dispatcher
override protected val log: LoggingAdapter = Logging(system, getClass)
override protected implicit val materializer: ActorMaterializer = ActorMaterializer()
Http().bindAndHandle(routes(processor, view), httpInterface, httpPort)
}
trait BaseServiceRoute {
protected implicit def executor: ExecutionContext
protected implicit def materializer: ActorMaterializer
protected def log: LoggingAdapter
}
trait MainActorSystem {
implicit val system = ActorSystem("booking")
}
final case class CalculatePriceForRangeDto(unitId: Int, from: Long, to: Long)
trait PriceServiceRoute extends BaseServiceRoute {
implicit val timeout = Timeout(30 seconds)
import com.example.crudapi.utils.MarshallingSupport._
def customersRoute(command: ActorRef, query: ActorRef) = pathPrefix("price") {
post {
path("calculate") {
decodeRequest {
entity(as[CalculatePriceForRangeDto]) {
priceForRange => onComplete((query ? CalculatePriceForRange(
但我越来越
Error:(32, 20) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[com.example.crudapi.http.routes.CalculatePriceForRangeDto]
entity(as[CalculatePriceForRangeDto]) {
^
已经看到所有相关的SO问题,但没有解决我的问题。奇怪的部分是,我试过Typesafe模板akka-dddd-cqrs,它的工作原理,相同的代码。
我是否缺少隐式上下文? 任何想法可能是什么?
答
SprayJsonSupport
适用于spray-json(不适用于json4s)。因此,你需要定义marshallers如下
trait JsonMarshallers extends DefaultJsonProtocol {
implicit val DigestItemWireFormat = jsonFormat6(DigestItemWire.apply)
implicit val LocalDateTimeFormat = new JsonFormat[LocalDateTime] {
private val iso_date_time = DateTimeFormatter.ISO_DATE_TIME
def write(x: LocalDateTime) = JsString(iso_date_time.format(x))
def read(value: JsValue) = value match {
case JsString(x) => LocalDateTime.parse(x, iso_date_time)
case x => throw new RuntimeException(s"Unexpected type %s on parsing of LocalDateTime type".format(x.getClass.getName))
}
}
}
,然后在你使用它们或将其与PriceServiceRoute
混合,并在文件的开头导入akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
范围导入JsonMarshallers._
。
什么样的类是DigestItemWire?没有在范围内。 – Reeebuuk
@Reeebuuk这就是您想要序列化/反序列化的案例类的例子,就像您的案例中的“CalculatePriceForRangeDto”一样。 – expert
试过,但我得到错误:类型不匹配; found:(Int,Long,Long)=> com.example.crudapi.http.routes.CalculatePriceForRangeDto required:(?,?,?,?,?,?)=>? 注:内含价值CalculatePriceForRangeDtoFormat这里不适用,因为它是应用点之后,并没有一个明确的结果类型 隐含VAL CalculatePriceForRangeDtoFormat = jsonFormat6(CalculatePriceForRangeDto.apply) – Reeebuuk