首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何创建一个在游戏中打印整个请求和响应的方法?

如何创建一个在游戏中打印整个请求和响应的方法?
EN

Stack Overflow用户
提问于 2018-04-07 17:42:53
回答 1查看 35关注 0票数 0

我想创建一个帮助器方法,它可以从Controller调用,并打印Request的不同参数。我试图像下面这样做,但我需要将类型参数传递给Request。我应该为类型参数传递什么?

代码语言:javascript
复制
import play.api.mvc.Request

package object utilities {

  def printPlayHttpRequest (request:Request): Unit ={ //Request needs type parameter. What should I pass as type parameter?

  }
}

查看Request对象的apply方法,似乎传递的类型参数对应于主体类型。那么我需要传递一些体型吗?

代码语言:javascript
复制
def apply[A](rh: RequestHeader, body: A): Request[A] 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-07 17:49:56

根据Play文档,我认为Request[A]中的A对应于body类型,它可以是请求body的任何Scala类型,例如String、NodeSeq、ArrayByte、JsonValue或java.io.File,只要我们有能够处理它的body解析器

来自文档

代码语言:javascript
复制
Previously we said that an Action was a Request => Result function. This is not entirely true. Let’s have a more precise look at the Action trait:

trait Action[A] extends (Request[A] => Result) {
  def parser: BodyParser[A]
}

First we see that there is a generic type A, and then that an action must define a BodyParser[A]. With Request[A] being defined as:

trait Request[+A] extends RequestHeader {
  def body: A
}

The A type is the type of the request body. We can use any Scala type as the request body, for example String, NodeSeq, Array[Byte], JsonValue, or java.io.File, as long as we have a body parser able to process it.

To summarize, an Action[A] uses a BodyParser[A] to retrieve a value of type A from the HTTP request, and to build a Request[A] object that is passed to the action code.

下面是我的实现

代码语言:javascript
复制
import play.api.mvc.{AnyContent, Request}

package object utilities {

  def printPlayHttpRequest (request:Request[AnyContent]): Unit ={
    println(s"Attr: ${request.attrs}, \n Body: ${request.body},  \n connection: ${request.connection},  \n headers: ${request.headers},  \n method: ${request.method}, \n target:  ${request.target},  \n version: ${request.version},  \n mediatype: ${request.mediaType}")
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49705977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档