当前位置: 动力学知识库 > 问答 > 编程问答 >

marshalling - Using akka-http `oneOf` marshaller to return either of two MIME types

问题描述:

I've got an end point that I wish could provide either JSON or HTML version of the same data, depending on how the client's Accept header prefers.

This should be easy with akka-http (2.4.10) Marshalling support, but I don't get it to work.

The code:

val genHtml = () => HttpEntity( ContentTypes.`text/html(UTF-8)`, s"<!DOCTYPE html><html><p style:\"color:$color\">$color</p></html> " )

val genJson = () => JsObject( "color" -> JsString(color.s))

The Marshall.oneOf method is the one I should use, but how? I haven't found an actual sample online, yet. There's Marshaller, Marshallable and Marshalling.

In the one-off case, I just pass a JsObject and the spray-akka-http bridging has a ToResponseMarshaller for it.

分享给朋友:
您可能感兴趣的文章:
随机阅读: