I have a Play framework controller method that returns either a Byte Array or a String based on the request header. Here is what it looks like:
def returnResponse = Action(parse.anyContent) {
request =>
println(request.body)
val buffer: RawBuffer = request.body.asRaw.get
val js: String = buffer.asBytes() match {
case Some(x) => new String(x, "UTF-8")
case None => scala.io.Source.fromFile(buffer.asFile).mkString
}
val resultJsonfut = scala.concurrent.Future { serviceCall.run(js) }
Async {
resultJsonfut.map(s => {
val out = if(request.headers.toSimpleMap.exists(_ == (CONTENT_ENCODING, "gzip"))) getBytePayload(s) else s
Ok(out)
})
}
}
I do not see any error in IntelliJ, but when I compile it, it fails with the following error:
Cannot write an instance of java.io.Serializable to HTTP response. Try to define a Writeable[java.io.Serializable]
Why is that? But however if I modify it a little bit to look like as below:
Async {
if(request.headers.toSimpleMap.exists(_ == (CONTENT_ENCODING, "gzip"))) {
resultJsonfut.map(s => Ok(getBytePayload(s)))
} else {
resultJsonfut.map(s => Ok(s))
}
}
It compiles fine. Any reasons why it behaves this way?