如何在play 2.6中使用带有标题的流

时间:2019-02-07 12:39:40

标签: scala playframework playframework-2.6

最近我从游戏2.4升级到了2.6 我有此代码

def prometheusMetrics = Action {
    val responseStream = Concurrent.unicast[Array[Byte]] { channel =>
      val writer = new WriterAdapter(channel)
      TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples())
      writer.close()
    }
    Ok.stream(responseStream).withHeaders("Content-Type" -> TextFormat.CONTENT_TYPE_004)
  )

  }

在2.6版中,它会生成编译时错误

myproject / app / controllers / Application.scala:146:值流不是Application.this.Status的成员 [错误] Ok.stream(responseStream).withHeaders(“ Content-Type”-> TextFormat.CONTENT_TYPE_004)

所以我更改了这样的代码

def prometheusMetrics = Action {
    val responseStream = Concurrent.unicast[Array[Byte]] { channel =>
      val writer = new WriterAdapter(channel)
      TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples())
      writer.close()
    }
    Result(
    header = ResponseHeader(200 -> Map("Content-Type" -> TextFormat.CONTENT_TYPE_004)),
    body = HttpEntity.Streamed(responseStream)
  )
  }

但再次给出了编译时错误

myproject/app/controllers/Application.scala:148: type mismatch;
[error]  found   : (Int, scala.collection.immutable.Map[String,String])
[error]  required: Int
[error]     header = ResponseHeader(200 -> Map("Content-Type" -> TextFormat.CONTENT_TYPE_004)),
[error]                                 ^

我怎么添加

"Content-Type" -> TextFormat.CONTENT_TYPE_004

在这里作为标题?正确的方法是什么?

0 个答案:

没有答案