pekko-http thrift and json marshalling/unmarshalling for Thrift structs
| Version | Release date | pekko Http version | Thrift version | Scala versions |
|---|---|---|---|---|
1.1.0 |
2024-04-09 | 1.0.1 |
0.20.0 |
3.3, 2.13 |
1.0.0 |
2023-11-02 | 1.0.0 |
0.19.0 |
3.3, 2.13 |
The complete list can be found in the CHANGELOG file.
Libraries are published to Maven Central. Add to your build.sbt:
libraryDependencies += "fr.davit" %% "pekko-http-thrift" % <version> // thrift supportFor the examples, we are using the following thrift domain model
struct Item {
1: string name
2: i64 id
}
struct Order {
1: list<Item> items
}Marshalling/Unmarshalling of the generated classes depends on the Accept/Content-Type header sent by the client:
Content-Type: application/json: jsonContent-Type: application/vnd.apache.thrift.json: jsonContent-Type: application/vnd.apache.thrift.binary: binaryContent-Type: application/vnd.apache.thrift.compact: compact
-No Accept header or matching several (eg Accept: application/*) will take the 1st matching type from the above
list.
The implicit marshallers and unmarshallers for your generated thrift classes are defined in
ThriftSupport. Specific (un)marshallers can be imported from ThriftBinarySupport, ThriftCompactSupport
and ThriftJsonSupport.
You simply need to have them in scope.
import org.apache.pekko.http.scaladsl.server.Directives
import fr.davit.pekko.http.scaladsl.marshallers.thrift.ThriftSupport
class MyThriftService extends Directives with ThriftSupport {
// format: OFF
val route =
get {
pathSingleSlash {
complete(Item("thing", 42))
}
} ~
post {
entity(as[Order]) { order =>
val itemsCount = order.items.size
val itemNames = order.items.map(_.name).mkString(", ")
complete(s"Ordered $itemsCount items: $itemNames")
}
}
// format: ON
}Entity streaming (http chunked transfer) is at the moment not supported by the library.