如何创建一个在 Play 中打印整个请求和响应的方法?
How to create a method which prints the entire Request and Response in Play?
我想创建一个可以从 Controller
调用并打印 Request
的不同参数的辅助方法。我尝试执行如下操作,但我需要将类型参数传递给 Request
。我应该为类型参数传递什么?
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
方法,好像传递的type参数对应的是Body类型。那么我需要传递一些体型吗?
def apply[A](rh: RequestHeader, body: A): Request[A]
根据 Play 文档,我认为 Request[A]
中的 A
对应于主体类型,它可以是任何 Scala 类型作为请求主体,例如 String、NodeSeq、Array[Byte] 、JsonValue 或 java.io.File,只要我们有一个能够处理它的正文解析器
来自文档
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.
以下是我的实现
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}")
}
}
我想创建一个可以从 Controller
调用并打印 Request
的不同参数的辅助方法。我尝试执行如下操作,但我需要将类型参数传递给 Request
。我应该为类型参数传递什么?
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
方法,好像传递的type参数对应的是Body类型。那么我需要传递一些体型吗?
def apply[A](rh: RequestHeader, body: A): Request[A]
根据 Play 文档,我认为 Request[A]
中的 A
对应于主体类型,它可以是任何 Scala 类型作为请求主体,例如 String、NodeSeq、Array[Byte] 、JsonValue 或 java.io.File,只要我们有一个能够处理它的正文解析器
来自文档
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.
以下是我的实现
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}")
}
}