我正在尝试在单个POST调用中将JSON
映射到多个案例类
val route = (path(HttpConstants.CreateJob) & post) {
entity(as[JobDetailsEntity]) {
jobDetailsEntity: JobDetailsEntity =>
complete(createJobDetails(updatedJobDetailsEntity).map(_.asJson))
} ~
entity(as[JobEntity]) { jobEntity: JobEntity =>
complete(createJob(updatedJobEntity).map(_.asJson))
}
}
这样做只会执行createJobDetails
而不是createJob
。
case class JobDetailsEntity(id:Option[Long],name:String,data:String,createDate:Long)
case class JobEntity(id:Option[Long],name:String,createDate:Long)
虽然我可以在具有不同路径路径的单独POST调用中执行此操作。如果有办法在单个REST调用中执行此操作,请帮助我。
答案 0 :(得分:0)
使用~
(或concat
)concatenate 2 routes,基本上使第二个成为第一个回退,以防第一个未成功完成。
您所寻找的内容相当于将指令嵌套在一条路线中。您可以使用&
(正如您在指令path
和post
上所做的那样)来实现此结果。
val route = (path(HttpConstants.CreateJob) & post) {
(entity(as[JobDetailsEntity]) & entity(as[JobEntity])) {
(jobDetailsEntity: JobDetailsEntity, jobEntity: JobEntity) =>
complete(...)
}
}
答案 1 :(得分:0)
任何人都可以在这里提供帮助。用途如下:
post {
entity(as[Either[JobDetailsEntity, JobEntity]]) {
case left: Left[JobDetailsEntity, JobEntity] => doSomethingWith(left.value)
case right: Right[JobDetailsEntity, JobEntity] => doSomethingWith(right.value)
}
}