An HTTP request (at least for those using the POST and PUT operations) contains a body. This body can be formatted with any format specified in the Content-Type header. A body parser transforms this request body into a Java value.json
一個HTTP請求包含一個Body(至少POST和PUT如此)。這個body能夠按照指定的內容類型格式化。mvc
一個Body解析器負責把請求的Body轉換成Java值。app
Note: You can’t write
BodyParser
implementation directly using Java. Because a PlayBodyParser
must handle the body content incrementaly using anIteratee[Array[Byte], A]
it must be implemented in Scala.ide注意:
BodyParser
不能用Java實現,只能用scala實現,這是由於BodyParser
用來處理漸增數據會用到scala實現的Iteratee[Array[Byte], A]
。this
However Play provides default
BodyParser
s that should fit most use cases (parsing Json, Xml, Text, uploading files). And you can reuse these default parsers to create your own directly in Java; for example you can provide an RDF parsers based on the Text one.url然而,play提供了適合絕大多數狀況下的默認
BodyParser
s (解析Json, Xml, Text, 上載文件)spa
BodyParser
Java APIIn the Java API, all body parsers must generate a play.mvc.Http.RequestBody
value. This value computed by the body parser can then be retrieved viarequest().body()
:scala
在Java API中,全部的body解析器都會生成一個 play.mvc.Http.RequestBody
code
pulic static Result index() { RequestBody body = request().body(); ok("Got body: " + body); }
You can specify the BodyParser
to use for a particular action using the@BodyParser.Of
annotation:orm
@BodyParser.Of(BodyParser.Json.class) pulic static Result index() { RequestBody body = request().body(); ok("Got json: " + body.asJson()); }
Http.RequestBody
APIAs we just said all body parsers in the Java API will give you aplay.mvc.Http.RequestBody
value. From this body object you can retrieve the request body content in the most appropriate Java type.
就像剛纔說的那樣,全部body解析器都會獲得一個play.mvc.Http.RequestBody
值,咱們能夠從這個body對象裏得到適當類型body的內容。
Note: The
RequestBody
methods likeasText()
orasJson()
will return null if the parser used to compute this request body doesn’t support this content type. For example in an action method annotated with@BodyParser.Of(BodyParser.Json.class)
, callingasXml()
on the generated body will retun null.注意:若是解析器不支持請求body的類型,
asText()
或asJson()
等RequestBody
的一些方法會返回null。例如一個action用了@BodyParser.Of(BodyParser.Json.class)
註解,若是調用asXml()就會返回null。
Some parsers can provide a most specific type than Http.RequestBody
(ie. a subclass of Http.RequestBody
). You can automatically cast the request body into another type using the as(...)
helper method:
一些解析器可能會提供最經常使用的類型(例如 Http.RequestBody
的子類)。你能夠用 as(...)
方法進行轉換。
@BodyParser.Of(BodyLengthParser.class)
pulic static Result index() {
BodyLength body = request().body().as(BodyLength.class);
ok("Request body length: " + body.getLength());
}
If you don’t specify your own body parser, Play will use the default one guessing the most appropriate content type from the Content-Type
header:
若是你不指定body解析器,play就會用默認的來推測類型:
String
, accessible via asText()
JsonNode
, accessible via asJson()
org.w3c.Document
, accessible via asXml()
Map<String, String[]>
, accessible viaasFormUrlEncoded()
Http.MultipartFormData
, accessible viaasMultipartFormData()
Http.RawBuffer
, accessible via asRaw()
Example:
pulic static Result save() { RequestBody body = request().body(); String textBody = body.asText(); if(textBody != null) { ok("Got: " + text); } else { badRequest("Expecting text/plain request body"); } }
Text based body parsers (such as text, json, xml or formUrlEncoded) use a max content length because they have to load all the content into memory.
基於文本的body解析器(例如 text, json, xml 或者 formUrlEncoded) 須要使用最大content長度,由於它們會把全部的內容加載到內存裏。
There is a default content length (the default is 100KB).
這是個默認的內容長度(100KB).
Tip: The default content size can be defined in
application.conf
:提示:默認的content大小能夠在application.conf 中定義:
parsers.text.maxLength=128K
You can also specify a maximum content length via the @BodyParser.Of
annotation:
你也能夠經過 @BodyParser.Of
註解來制定最大content長度:
}
}
// Accept only 10KB of data. @BodyParser.Of(value = BodyParser.Text.class, maxLength = 10 * 1024) pulic static Result index() { if(request().body().isMaxSizeExceeded()) { return badRequest("Too much data!"); } else { ok("Got body: " + request().body().asText());