Play Framework - Zip JSON请求

时间:2014-02-17 10:17:07

标签: json playframework websocket

我有一个问题,我无法从Play Framework的文档中找到足够的信息。我有一个非常大的JSON,我将这个JSON发送到WebSocket端点。我现在需要以某种方式从客户端压缩此JSON并将其发送到WebSocket端点。一旦服务器获得此压缩JSON,它就会解压缩并处理它。目前有什么方法可以使用Play Framework的任何功能吗?或者有一个我可以研究的图书馆吗?

我正在使用Play Framework 2.1.1。

以下是我将Zipped JSON请求发送到服务器的方法:

val input = Source.fromURL(getClass.getResource("/bulk_json_10000.txt")).mkString
val bulkJson: Array[Byte] = Utility.zipJson(input)

仅出于测试目的,我使用的HTTP库向服务器发送请求:

val result = Http.postData(url, bulkJson)
  .header("Content-Type", "application/json")
  .header("Charset", "UTF-8")
  .option(HttpOptions.readTimeout(10000))
  .responseCode
logger.info("The response from the server " + result)

但我看到的只是400作为响应代码。

添加Content-Encoding标头后如下:

.header("Content-Encoding", "gzip")

我能够摆脱错误的请求错误,现在服务器抛出"来自Server"文件的意外结束错误。

以下是我在控制器上的操作:

  def gzipTest = Action(parse.anyContent) { request =>
    println(request.body)
    Ok("Done!")
  }

我有什么遗失的吗?我甚至没有尝试在服务器端对进入的请求做任何事情。为什么服务器会失败?

在客户端上摆弄GZIPing请求之后,我将GZipFilter添加到我的Global.scala中,我看到以下错误,我看到的是从服务器抛出:

org.jboss.netty.handler.codec.embedder.CodecEmbedderException:org.jboss.netty.handler.codec.compression.CompressionException:解压缩失败(-3):不是org.jboss.netty.handler中的gzip流.codec.http.HttpContentDecoder.messageReceived(HttpContentDecoder.java:116)             在org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)             在org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:459)             在org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:536)             在org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:435)             在org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)             在org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)             在org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88)             在org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:107)             在org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:312)             在org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:88)             在org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)             at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)             at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)             在java.lang.Thread.run(未知来源)     引起:org.jboss.netty.handler.codec.compression.CompressionException:解压缩失败(-3):不是gzip流             在org.jboss.netty.handler.codec.compression.ZlibUtil.exception(ZlibUtil.java:31)             在org.jboss.netty.handler.codec.compression.ZlibUtil.fail(ZlibUtil.java:27)             在org.jboss.netty.handler.codec.compression.ZlibDecoder.decode(ZlibDecoder.java:152)             在org.jboss.netty.handler.codec.oneone.OneToOneDecoder.handleUpstream(OneToOneDecoder.java:66)             ......还有19个     java.nio.channels.ClosedChannelException             在org.jboss.netty.channel.socket.nio.AbstractNioWorker.cleanUpWriteBuffer(AbstractNioWorker.java:409)             在org.jboss.netty.channel.socket.nio.AbstractNioWorker.writeFromUserCode(AbstractNioWorker.java:127)             在org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:99)             在org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:36)             在org.jboss.netty.channel.Channels.write(Channels.java:725)             在org.jboss.netty.handler.codec.oneone.OneToOneEncoder.doEncode(OneToOneEncoder.java:71)             在org.jboss.netty.handler.codec.oneone.OneToOneEncoder.handleDownstream(OneToOneEncoder.java:59)             在org.jboss.netty.channel.Channels.write(Channels.java:704)             在org.jboss.netty.channel.Channels.write(Channels.java:671)             在org.jboss.netty.channel.AbstractChannel.write(AbstractChannel.java:248)             at play.core.server.netty.PlayDefaultUpstreamHandler $$ anon $ 2 $$ anonfun $ handle $ 6 $$ anonfun $ 12.apply(PlayDefaultUpstreamHandler.scala:208)             at play.core.server.netty.PlayDefaultUpstreamHandler $$ anon $ 2 $$ anonfun $ handle $ 6 $$ anonfun $ 12.apply(PlayDefaultUpstreamHandler.scala:204)             at play.api.libs.iteratee.Enumeratee $$ anon $ 50 $$ anon $ 16 $$ anonfun $ play $ api $ libs $ iteratee $ Enumeratee $$ anon $$ anon $$ step $ 2 $$ anonfun $ apply $ 13.apply( Enumeratee.scala:359)             at play.api.libs.iteratee.Enumeratee $$ anon $ 50 $$ anon $ 16 $$ anonfun $ play $ api $ libs $ iteratee $ Enumeratee $$ anon $$ anon $$ step $ 2 $$ anonfun $ apply $ 13.apply( Enumeratee.scala:359)             在scala.concurrent.Future $$ anonfun $ map $ 1.liftedTree2 $ 1(Future.scala:253)             在scala.concurrent.Future $$ anonfun $ map $ 1.apply(Future.scala:249)             在scala.concurrent.Future $$ anonfun $ map $ 1.apply(Future.scala:249)             在scala.concurrent.impl.CallbackRunnable.run(Promise.scala:29)             at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)             at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)             在java.lang.Thread.run(未知来源)

1 个答案:

答案 0 :(得分:1)

直接播放Framework 2.2.x supports Gzip。对于您的版本,您可以查看this related answer。如上所述,如果Play位于前端后面,您可以在前端执行gzip / gunzip。