play2.0文檔-面向java開發者(5)

Body parsers

Body解析器

What is a body parser?

body解析器是啥?


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 Play BodyParser 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 BodyParsers 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提供了適合絕大多數狀況下的默認 BodyParsers (解析Json, Xml, Text, 上載文件)spa

The BodyParser Java API

In the Java API, all body parsers must generate a play.mvc.Http.RequestBodyvalue. This value computed by the body parser can then be retrieved viarequest().body():scala

在Java API中,全部的body解析器都會生成一個 play.mvc.Http.RequestBodycode

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()); }

The Http.RequestBody API

As 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 like asText() or asJson() 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), calling asXml() 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());

}

Default body parser: AnyContent

默認的body解析器:AnyContent

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就會用默認的來推測類型:

  • text/plainString, accessible via asText()
  • application/jsonJsonNode, accessible via asJson()
  • text/xmlorg.w3c.Document, accessible via asXml()
  • application/form-url-encodedMap<String, String[]>, accessible viaasFormUrlEncoded()
  • multipart/form-dataHttp.MultipartFormData, accessible viaasMultipartFormData()
  • Any other content type: 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"); } }

Max content length

最大content長度

Text based body parsers (such as textjsonxml or formUrlEncoded) use a max content length because they have to load all the content into memory.

基於文本的body解析器(例如 textjsonxml 或者 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());
相關文章
相關標籤/搜索