Akka.http.scaladsl.marshalling.ToResponseMarshallable
lastmod 2026-11-10created 2020-08-263 edits
ToResponseMarshallable은 Akka HTTP 의 Scala DSL 에서 어떤 값과 그 값을 HTTP 응답으로 바꿔 줄 ToResponseMarshaller를 한데 묶은 트레이트다. 라우트의 complete() 지시자가 이 타입을 받기 때문에, 암시적 마샬러가 범위 안에 있는 값이면 문자열, case class, Future 무엇이든 그대로 넘겨 응답을 만들 수 있다. 타입을 지운 뒤 요청의 Accept 헤더에 맞춰 나중에 마샬링한다.
HTTP 응답(Respose) 으로, 마샬링 (Marshalling) 될 수 있는것.
Code
/*
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.http.scaladsl.marshalling
import scala.concurrent.{ Future, ExecutionContext }
import akka.http.scaladsl.model._
/** 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) }
}