Akka-http是一项系统融为一体工具。这首要借助系统里面的数据交流效率。因为程序内数据表明形式与网上传输的数码格式是不平等的,所以需要对先后高级结构化的数额开展转移(marshalling
or
serializing)成为可在网上传输的多少格式。由于可能波及到异类系统融为一体,网上传输数据格式是一个精晓的业内,这样我们才都可以拓展解析。Json就是是一个当下业界广泛接受的网上互换数据格式。当然,所谓的数码格式转换应该是双向的,还需要包括把吸收的网上传输数据转换成程序高级结构化数据。

Akka-http网上交流数据转换代表把一个高等结构类型T的实例转换成简单的对象项目如MessageEntity,它象征http音讯中的数据部分(entity-body),最后发生Json举行互换。Akka-http用马尔斯haller[A,B]品种来开展类型A的实例到花色B实例的变换。马尔斯(Mars)haller[A,B]概念如下:

sealed abstract class Marshaller[-A, +B] {

  def apply(value: A)(implicit ec: ExecutionContext): Future[List[Marshalling[B]]]

  def map[C](f: B ⇒ C): Marshaller[A, C] =
    Marshaller(implicit ec ⇒ value ⇒ this(value).fast map (_ map (_ map f)))
...
}

//#marshaller-creation
object Marshaller
  extends GenericMarshallers
  with PredefinedToEntityMarshallers
  with PredefinedToResponseMarshallers
  with PredefinedToRequestMarshallers {

  /**
   * Creates a [[Marshaller]] from the given function.
   */
  def apply[A, B](f: ExecutionContext ⇒ A ⇒ Future[List[Marshalling[B]]]): Marshaller[A, B] =
    new Marshaller[A, B] {
      def apply(value: A)(implicit ec: ExecutionContext) =
        try f(ec)(value)
        catch { case NonFatal(e) ⇒ FastFuture.failed(e) }
    }
...
}

构建函数apply[A,B]包嵌了个操作函数:A=>Future[List[Marshalling[B]]],至于怎么不行使更简便易行直接的不二法门A=>B是因为:

1、转换的进程是异步non-blocking的,所以回来Future[??]

2、可能有多种更换目的数据格式如XML,Json:所以用List[??]表达

3、假诺急需在暴发最终目标格式数据前能取得或者修改数据的属性,就需要在数据源与目的数据里面设一个中间层结果,马尔斯(Mars)halling[B]就是这样一个中档抽象层类型。通过马尔斯(Mars)halling类型可以在多少实际上暴发以前得到或涂改数据属性:

/**
 * Describes one possible option for marshalling a given value.
 */
sealed trait Marshalling[+A] {
  def map[B](f: A ⇒ B): Marshalling[B]

  /**
   * Converts this marshalling to an opaque marshalling, i.e. a marshalling result that
   * does not take part in content type negotiation. The given charset is used if this
   * instance is a `WithOpenCharset` marshalling.
   */
  def toOpaque(charset: HttpCharset): Marshalling[A]
}

object Marshalling {

  /**
   * A Marshalling to a specific [[akka.http.scaladsl.model.ContentType]].
   */
  final case class WithFixedContentType[A](
    contentType: ContentType,
    marshal:     () ⇒ A) extends Marshalling[A] {
    def map[B](f: A ⇒ B): WithFixedContentType[B] = copy(marshal = () ⇒ f(marshal()))
    def toOpaque(charset: HttpCharset): Marshalling[A] = Opaque(marshal)
  }

  /**
   * A Marshalling to a specific [[akka.http.scaladsl.model.MediaType]] with a flexible charset.
   */
  final case class WithOpenCharset[A](
    mediaType: MediaType.WithOpenCharset,
    marshal:   HttpCharset ⇒ A) extends Marshalling[A] {
    def map[B](f: A ⇒ B): WithOpenCharset[B] = copy(marshal = cs ⇒ f(marshal(cs)))
    def toOpaque(charset: HttpCharset): Marshalling[A] = Opaque(() ⇒ marshal(charset))
  }

  /**
   * A Marshalling to an unknown MediaType and charset.
   * Circumvents content negotiation.
   */
  final case class Opaque[A](marshal: () ⇒ A) extends Marshalling[A] {
    def map[B](f: A ⇒ B): Opaque[B] = copy(marshal = () ⇒ f(marshal()))
    def toOpaque(charset: HttpCharset): Marshalling[A] = this
  }
}

我们得以在马尔斯(Mars)halling类型里对消息内容类型(message-content-type)举办操作。为了便于操作,Akka-http提供了上面那些类型别名:

type ToEntityMarshaller[T] = Marshaller[T, MessageEntity]
type ToByteStringMarshaller[T] = Marshaller[T, ByteString]
type ToHeadersAndEntityMarshaller[T] = Marshaller[T, (immutable.Seq[HttpHeader], MessageEntity)]
type ToResponseMarshaller[T] = Marshaller[T, HttpResponse]
type ToRequestMarshaller[T] = Marshaller[T, HttpRequest]

大抵是以目标数据类型来分类代表的。Akka-http提供了很多档次的预设实例到Mashalling转换:

PredefinedToEntityMarshallers
Array[Byte]
ByteString
Array[Char]
String
akka.http.scaladsl.model.FormData
akka.http.scaladsl.model.MessageEntity
T <: akka.http.scaladsl.model.Multipart
PredefinedToResponseMarshallers
T, if a ToEntityMarshaller[T] is available
HttpResponse
StatusCode
(StatusCode, T), if a ToEntityMarshaller[T] is available
(Int, T), if a ToEntityMarshaller[T] is available
(StatusCode, immutable.Seq[HttpHeader], T), if a ToEntityMarshaller[T] is available
(Int, immutable.Seq[HttpHeader], T), if a ToEntityMarshaller[T] is available
PredefinedToRequestMarshallers
HttpRequest
Uri
(HttpMethod, Uri, T), if a ToEntityMarshaller[T] is available
(HttpMethod, Uri, immutable.Seq[HttpHeader], T), if a ToEntityMarshaller[T] is available
GenericMarshallers
Marshaller[Throwable, T]
Marshaller[Option[A], B], if a Marshaller[A, B] and an EmptyValue[B] is available
Marshaller[Either[A1, A2], B], if a Marshaller[A1, B] and a Marshaller[A2, B] is available
Marshaller[Future[A], B], if a Marshaller[A, B] is available
Marshaller[Try[A], B], if a Marshaller[A, B] is available

Akka-http还提供了一个工具类马尔斯hal:

class Marshal[A](val value: A) {
  /**
   * Marshals `value` using the first available [[Marshalling]] for `A` and `B` provided by the given [[Marshaller]].
   * If the marshalling is flexible with regard to the used charset `UTF-8` is chosen.
   */
  def to[B](implicit m: Marshaller[A, B], ec: ExecutionContext): Future[B] =
    m(value).fast.map {
      _.head match {
        case Marshalling.WithFixedContentType(_, marshal) ⇒ marshal()
        case Marshalling.WithOpenCharset(_, marshal)      ⇒ marshal(HttpCharsets.`UTF-8`)
        case Marshalling.Opaque(marshal)                  ⇒ marshal()
      }
    }

  /**
   * Marshals `value` to an `HttpResponse` for the given `HttpRequest` with full content-negotiation.
   */
  def toResponseFor(request: HttpRequest)(implicit m: ToResponseMarshaller[A], ec: ExecutionContext): Future[HttpResponse] = {
    import akka.http.scaladsl.marshalling.Marshal._
    val ctn = ContentNegotiator(request.headers)

    m(value).fast.map { marshallings ⇒
      val supportedAlternatives: List[ContentNegotiator.Alternative] =
        marshallings.collect {
          case Marshalling.WithFixedContentType(ct, _) ⇒ ContentNegotiator.Alternative(ct)
          case Marshalling.WithOpenCharset(mt, _)      ⇒ ContentNegotiator.Alternative(mt)
        }(collection.breakOut)
      val bestMarshal = {
        if (supportedAlternatives.nonEmpty) {
          ctn.pickContentType(supportedAlternatives).flatMap {
            case best @ (_: ContentType.Binary | _: ContentType.WithFixedCharset | _: ContentType.WithMissingCharset) ⇒
              marshallings collectFirst { case Marshalling.WithFixedContentType(`best`, marshal) ⇒ marshal }
            case best @ ContentType.WithCharset(bestMT, bestCS) ⇒
              marshallings collectFirst {
                case Marshalling.WithFixedContentType(`best`, marshal) ⇒ marshal
                case Marshalling.WithOpenCharset(`bestMT`, marshal)    ⇒ () ⇒ marshal(bestCS)
              }
          }
        } else None
      } orElse {
        marshallings collectFirst { case Marshalling.Opaque(marshal) ⇒ marshal }
      } getOrElse {
        throw UnacceptableResponseContentTypeException(supportedAlternatives.toSet)
      }
      bestMarshal()
    }
  }
}

咱俩得以用马尔斯hal.to和toResponseFor(request)把Akka-http提供的预设可转换类实例转换成相关的toResponse马尔斯hallable类实例。因为Server-Directive如complete接受一个toResponse马尔斯(Mars)hallable来构建HttpResponse:

  /**
   * Completes the request using the given arguments.
   *
   * @group route
   */
  def complete(m: ⇒ ToResponseMarshallable): StandardRoute =
    StandardRoute(_.complete(m))

在另一个目的里提供了ToResponse马尔斯hallable隐式转换:

/** Something that can later be marshalled into a response */
trait ToResponseMarshallable {
  type T
  def value: T
  implicit def marshaller: ToResponseMarshaller[T]

  def apply(request: HttpRequest)(implicit ec: ExecutionContext): Future[HttpResponse] =
    Marshal(value).toResponseFor(request)
}

object ToResponseMarshallable {
  implicit def apply[A](_value: A)(implicit _marshaller: ToResponseMarshaller[A]): ToResponseMarshallable =
    new ToResponseMarshallable {
      type T = A
      def value: T = _value
      def marshaller: ToResponseMarshaller[T] = _marshaller
    }

  implicit val marshaller: ToResponseMarshaller[ToResponseMarshallable] =
    Marshaller { implicit ec ⇒ marshallable ⇒ marshallable.marshaller(marshallable.value) }
}

若果在可视域内(implicit
scope)能觉察马尔斯haller[A,B]的隐式实例就能满意complete入参要求了。下面是部分马尔斯(Mars)hal用例:

import akka.util.ByteString
import akka.http.scaladsl.model.{HttpResponse, MessageEntity}
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model._

object Marshalling  {

  val string = "Yeah"
  val entityFuture = Marshal(string).to[MessageEntity]
  val errorMsg = "Easy, pal!"
  val responseFuture = Marshal(420 -> errorMsg).to[HttpResponse]
  val request = HttpRequest(headers = List(headers.Accept(MediaTypes.`application/json`)))
  val responseText = "Plaintext"
  val respFuture = Marshal(responseText).toResponseFor(request) // with content negotiation!
  val bsFuture = Marshal("oh my!").to[ByteString]
  val reqFuture = Marshal("can you?").to[HttpRequest]
  val resp = reqFuture.flatMap {r => Marshal("ok").toResponseFor(r)}

}

那么对于那个自定义的类型U,由于不能有预设定对应的马尔斯haller[U,B],应该如何是好?如简单的case
class:

  case class User(id: Int, name: String)
  case class Item(id: Int, name: String, price: Double)
  val john = Marshal(User(1,"John")).to[MessageEntity]
  val fruit = Marshal(Item(1,"banana", 3.5)).to[MessageEntity]

  val route =
    get {
      path("items") {
        complete(fruit)
      } ~
      path("users") {
        complete(john)
      }
    }

总的看把User,Item类型实例转成ToResponse马尔斯hallable是从未有过问题的。可是,通过隐式转换ToResponse马尔斯hallable被转换成马尔斯haller[U,B],而实例化这些类型的长河即构建网上传输格式的数目时需要更多的支撑。这多少个可网上传输的消息是由此Json、XML这样的数额描述语言来爆发实际多少的。Akka-http通过akka-http-spray-json模块直接协助由Spray-Json实现的Json读写工具库。具体Json读写是由此RootJsonFormat[T]用作接口实现的:

/**
 * A special JsonFormat signaling that the format produces a legal JSON root object, i.e. either a JSON array
 * or a JSON object.
 */
trait RootJsonFormat[T] extends JsonFormat[T] with RootJsonReader[T] with RootJsonWriter[T]

RootJsonFormat[T]代表T类型实例的Json转换。RootJsonFormat[T]的继承父辈包括:
/**
  * Provides the JSON deserialization and serialization for type T.
 */
trait JsonFormat[T] extends JsonReader[T] with JsonWriter[T]

/**
 * A special JsonReader capable of reading a legal JSON root object, i.e. either a JSON array or a JSON object.
 */
@implicitNotFound(msg = "Cannot find RootJsonReader or RootJsonFormat type class for ${T}")
trait RootJsonReader[T] extends JsonReader[T]

/**
 * A special JsonWriter capable of writing a legal JSON root object, i.e. either a JSON array or a JSON object.
 */
@implicitNotFound(msg = "Cannot find RootJsonWriter or RootJsonFormat type class for ${T}")
trait RootJsonWriter[T] extends JsonWriter[T]

它们又持续了实际的Json读写工具类:

/**
  * Provides the JSON deserialization for type T.
 */
@implicitNotFound(msg = "Cannot find JsonReader or JsonFormat type class for ${T}")
trait JsonReader[T] {
  def read(json: JsValue): T
}

object JsonReader {
  implicit def func2Reader[T](f: JsValue => T): JsonReader[T] = new JsonReader[T] {
    def read(json: JsValue) = f(json)
  }
}

/**
  * Provides the JSON serialization for type T.
 */
@implicitNotFound(msg = "Cannot find JsonWriter or JsonFormat type class for ${T}")
trait JsonWriter[T] {
  def write(obj: T): JsValue
}

object JsonWriter {
  implicit def func2Writer[T](f: T => JsValue): JsonWriter[T] = new JsonWriter[T] {
    def write(obj: T) = f(obj)
  }
}

它们提供了函数JsValue=>T到JsonReader[T]及T=>JsValue到JsonWriter直接的隐式转换。Akka-http的Json解决方案是优秀的type-class模式:是一种可以任意创造效用的系列继承格局(add-hoc
polymorphism)。它的特征就是在可视域内(implicit
scope)应不同效能要求提供不同的坚守实现项目标隐式实例(implicit
instance)。具体用例如下:

trait Formats extends SprayJsonSupport with DefaultJsonProtocol
object Converters extends Formats {
  case class User(id: Int, name: String)
  case class Item(id: Int, name: String, price: Double)
  implicit val itemFormat = jsonFormat3(Item.apply)
  implicit val userFormat = jsonFormat2(User.apply)
}

jsonFormatXX是Spray-Json提供的Json读写实现。咱们把这一个隐式实例置于当前可视域内即成功了与Akka-http的接入。大家来探视JsonFormat的概念:

trait ProductFormatsInstances { self: ProductFormats with StandardFormats =>
  // Case classes with 1 parameters

  def jsonFormat1[P1 :JF, T <: Product :ClassManifest](construct: (P1) => T): RootJsonFormat[T] = {
    val Array(p1) = extractFieldNames(classManifest[T])
    jsonFormat(construct, p1)
  }
  def jsonFormat[P1 :JF, T <: Product](construct: (P1) => T, fieldName1: String): RootJsonFormat[T] = new RootJsonFormat[T]{
    def write(p: T) = {
      val fields = new collection.mutable.ListBuffer[(String, JsValue)]
      fields.sizeHint(1 * 2)
      fields ++= productElement2Field[P1](fieldName1, p, 0)
      JsObject(fields: _*)
    }
    def read(value: JsValue) = {
      val p1V = fromField[P1](value, fieldName1)
      construct(p1V)
    }
  }
...
}

大家看出了jsonFormat再次回到结果类型是RootJsonFormat[T]。假诺有个case
class T,通过jsonFormat可以赢得read(value:
JsValue)及write(p:T)那五个具体的Json读写函数。Spray-Json提供的预设了Json转换的类别包括下面各类别:

/**
  * Provides all the predefined JsonFormats.
 */
trait DefaultJsonProtocol
        extends BasicFormats
        with StandardFormats
        with CollectionFormats
        with ProductFormats
        with AdditionalFormats

object DefaultJsonProtocol extends DefaultJsonProtocol

例如BasicFormat:

/**
  * Provides the JsonFormats for the most important Scala types.
 */
trait BasicFormats {

  implicit object IntJsonFormat extends JsonFormat[Int] {
    def write(x: Int) = JsNumber(x)
    def read(value: JsValue) = value match {
      case JsNumber(x) => x.intValue
      case x => deserializationError("Expected Int as JsNumber, but got " + x)
    }
  }
...
}

这一个品种的Json转换已经是切实可行的read/write操作了。在SprayJsonSupport
trait里有最终的马尔斯(Mars)haller[U,B]链接:

/**
 * A trait providing automatic to and from JSON marshalling/unmarshalling using an in-scope *spray-json* protocol.
 */
trait SprayJsonSupport {
...
  implicit def sprayJsonUnmarshaller[T](implicit reader: RootJsonReader[T]): FromEntityUnmarshaller[T] =
    sprayJsValueUnmarshaller.map(jsonReader[T].read)
...
  //#sprayJsonMarshallerConverter
  implicit def sprayJsonMarshaller[T](implicit writer: RootJsonWriter[T], printer: JsonPrinter = CompactPrinter): ToEntityMarshaller[T] =
    sprayJsValueMarshaller compose writer.write
...
}

我们在地点提到过FromEntityUnmarshaller[T]和ToEntityMarshaller[T]的是Marshaller[A,B]的别名:

  type FromEntityUnmarshaller[T] = Unmarshaller[HttpEntity, T]

  type ToEntityMarshaller[T] = Marshaller[T, MessageEntity]

既然Akka-http的Json实现形式是type-class形式的,那么大家就摸索此外Json库的功效实现格局,如:Json4s。大家需要在build.sbt中进入下边的看重性:

  "de.heikoseeberger" %% "akka-http-json4s" % "1.19.0-M2",
  "org.json4s" %% "json4s-jackson" % "3.6.0-M1",
  "org.json4s" %% "json4s-ext" % "3.6.0-M1",

 akka-http-Json4s通过trait
Json4sSupport提供了Json4s实现情势:

trait Json4sSupport {
...
  /**
    * HTTP entity => `A`
    *
    * @tparam A type to decode
    * @return unmarshaller for `A`
    */
  implicit def unmarshaller[A: Manifest](implicit serialization: Serialization,
                                         formats: Formats): FromEntityUnmarshaller[A] = ...

  /**
    * `A` => HTTP entity
    *
    * @tparam A type to encode, must be upper bounded by `AnyRef`
    * @return marshaller for any `A` value
    */
  implicit def marshaller[A <: AnyRef](implicit serialization: Serialization,
                                       formats: Formats,
                                       shouldWritePretty: ShouldWritePretty =
                                         ShouldWritePretty.False): ToEntityMarshaller[A] = ...

同一提供了FromEntityUnMarshaller[A]和ToEntityMarshaller[A]这两类的隐式实例。Serialization提供了Json的实际读写函数:

trait Serialization {
  import java.io.{Reader, Writer}
  /** Serialize to String.
   */
  def write[A <: AnyRef](a: A)(implicit formats: Formats): String
...
  /** Deserialize from a String.
   */
  def read[A](json: String)(implicit formats: Formats, mf: Manifest[A]): A = read(StringInput(json))
...
}

Formats就是Json4s提供的兼具Json转换预设类:

trait Formats extends Serializable { self: Formats =>
...
  def withBigInt: Formats = copy(wWantsBigInt = true)

  def withLong: Formats = copy(wWantsBigInt = false)

  def withBigDecimal: Formats = copy(wWantsBigDecimal = true)

...
}

看起来我们只需在可视域内提供Serialization和Formats类型的隐式实例就行了:

import de.heikoseeberger.akkahttpjson4s.Json4sSupport
import org.json4s.jackson
trait JsonCodec extends Json4sSupport {
  import org.json4s.DefaultFormats
  import org.json4s.ext.JodaTimeSerializers
  implicit val serilizer = jackson.Serialization
  implicit val formats = DefaultFormats ++ JodaTimeSerializers.all
}
object JsConverters extends JsonCodec

看看实际用例:

  import scala.collection.mutable._
  case class User(id: Int, name: String)
  class Item(id: Int, name: String, price: Double)
  object AnyPic {
    val area = 10
    val title = "a picture"
    val data = ArrayBuffer[Byte](1,2,3)
  }

  val john = Marshal(User(1,"John")).to[MessageEntity]
  val fruit = Marshal(new Item(1,"banana", 3.5)).to[MessageEntity]
  val pic = Marshal(AnyPic).to[MessageEntity]

不但省却了再次的JsonFormatXX,而且效果更加灵敏有力:因为不再局限于case
class这一种自定义类型了,在无需额外代码情形下class,object等全方位都辅助。

下边是本篇研究示范的源代码:

build.sbt

name := "learn-http"

version := "0.1"

scalaVersion := "2.12.3"

libraryDependencies ++= Seq(
  "de.heikoseeberger" %% "akka-http-json4s" % "1.19.0-M2",
  "org.json4s" %% "json4s-jackson" % "3.6.0-M1",
  "org.json4s" %% "json4s-ext" % "3.6.0-M1",
  "com.typesafe.akka" %% "akka-http" % "10.0.10",
  "com.typesafe.akka" %% "akka-actor" % "2.5.4",
  "com.typesafe.akka" %% "akka-stream" % "2.5.4",
  "com.typesafe.akka" %% "akka-http-spray-json" % "10.0.10"
)

Marshalling

import akka.actor._
import akka.stream._
import akka.util.ByteString
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.marshallers.sprayjson._
import spray.json._

trait Formats extends SprayJsonSupport with DefaultJsonProtocol
object Converters extends Formats {
  case class User(id: Int, name: String)
  case class Item(id: Int, name: String, price: Double)
  implicit val itemFormat = jsonFormat3(Item.apply)
  implicit val userFormat = jsonFormat2(User.apply)
}


object Marshalling {
  import Converters._
  implicit val httpSys = ActorSystem("httpSystem")
  implicit val httpMat = ActorMaterializer()
  implicit val httpEC = httpSys.dispatcher



  val string = "Yeah"
  val entityFuture = Marshal(string).to[MessageEntity]
  val errorMsg = "Easy, pal!"
  val responseFuture = Marshal(420 -> errorMsg).to[HttpResponse]
  val request = HttpRequest(headers = List(headers.Accept(MediaTypes.`application/json`)))
  val responseText = "Plaintext"
  val respFuture = Marshal(responseText).toResponseFor(request)

//  val bsFuture = Marshal("oh my!").to[ByteString]
  //  val reqFuture = Marshal(400).to[HttpRequest]
  //  val resp = reqFuture.flatMap {r => Marshal("ok").toResponseFor(r)}


  val john = Marshal(User(1,"John")).to[MessageEntity]
  val fruit = Marshal(Item(1,"banana", 3.5)).to[MessageEntity]

  val route =
    get {
      path("items") {
        complete(fruit)
      } ~
        path("users") {
          complete(john)
        }
    }

}

 Json4sMarshalling

import akka.actor._
import akka.stream._
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import de.heikoseeberger.akkahttpjson4s.Json4sSupport
import org.json4s.jackson
trait JsonCodec extends Json4sSupport {
  import org.json4s.DefaultFormats
  import org.json4s.ext.JodaTimeSerializers
  implicit val serilizer = jackson.Serialization
  implicit val formats = DefaultFormats ++ JodaTimeSerializers.all
}
object JsConverters extends JsonCodec


object Json4sMarshalling {
  import JsConverters._
  implicit val httpSys = ActorSystem("httpSystem")
  implicit val httpMat = ActorMaterializer()
  implicit val httpEC = httpSys.dispatcher

  val string = "Yeah"
  val entityFuture = Marshal(string).to[MessageEntity]
  val errorMsg = "Easy, pal!"
  val responseFuture = Marshal(420 -> errorMsg).to[HttpResponse]
  val request = HttpRequest(headers = List(headers.Accept(MediaTypes.`application/json`)))
  val responseText = "Plaintext"
  val respFuture = Marshal(responseText).toResponseFor(request)




  import scala.collection.mutable._
  case class User(id: Int, name: String)
  class Item(id: Int, name: String, price: Double)
  object AnyPic {
    val area = 10
    val title = "a picture"
    val data = ArrayBuffer[Byte](1,2,3)
  }

  val john = Marshal(User(1,"John")).to[MessageEntity]
  val fruit = Marshal(new Item(1,"banana", 3.5)).to[MessageEntity]
  val pic = Marshal(AnyPic).to[MessageEntity]

  val route =
    get {
      path("items") {
        complete(fruit)
      } ~
        path("users") {
          complete(john)
        } ~
      path("pic") {
        complete(pic)
      }
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相关文章

网站地图xml地图