DevilKing's blog

冷灯看剑,剑上几分功名?炉香无需计苍生,纵一穿烟逝,万丈云埋,孤阳还照古陵

0%

Akka Json unmarshal

原文链接

the usual library is spray-json

需要引入如下的lib

1
2
3
4
5
6
7
8
import akka.http.scaladsl.server.Directives._
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import io.circe.generic.auto._
def route = path("todos") {
get {
complete(todoRepository.all())
}
}

这部分主要是用来unmarshal Json

1
2
3
4
5
6
7
8
9
def route = path("todos") {
get {
complete(todoRepository.all())
} ~ post {
entity(as[Todo]) { todo =>
complete(todoRepository.save(todo))
}
}
}

使用entity,可以自动parse输入的json到我们的model