play framework2学习之旅<2.5>Body parsers
来源:互联网 发布:mac xquartz 编辑:程序博客网 时间:2024/05/15 19:41
Body parsers(body解析器)
What is a body parser?(还是那句话,认真读第一段内容介绍!)
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.(这是最关键的一句话!!!!)
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.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.
The BodyParser
Java API
In 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()
:
public static Result index() { RequestBody body = request().body(); return ok("Got body: " + body);}
You can specify the BodyParser
to use for a particular action using the@BodyParser.Of
annotation:
@BodyParser.Of(BodyParser.Json.class)public static Result index() { RequestBody body = request().body(); return 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.
这点要十分注意!!!这个是可以背过的O(∩_∩)O
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.
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:
@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(这个遇到了去查吧,留个印象,浏览带过)
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:
- text/plain:
String
, accessible viaasText()
- application/json:
JsonNode
, accessible viaasJson()
- text/xml:
org.w3c.Document
, accessible viaasXml()
- application/form-url-encoded:
Map<String, String[]>
, accessible viaasFormUrlEncoded()
- multipart/form-data:
Http.MultipartFormData
, accessible viaasMultipartFormData()
- Any other content type:
Http.RawBuffer
, accessible viaasRaw()
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(知道有这个东西,知道在哪去改即可)
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.
There is a default content length (the default is 100KB).
Tip: The default content size can be defined in
application.conf
:
parsers.text.maxLength=128K
You can also specify a maximum content length via the @BodyParser.Of
annotation:
// 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()); }}
- play framework2学习之旅<2.5>Body parsers
- play framework2学习之旅<1>
- play framework2学习之旅<2.1>Actions, Controllers and Results
- play framework2学习之旅<2.2>HTTP routing
- play framework2学习之旅<2.3>Manipulating the response
- play framework2学习之旅<2.4>Session and Flash scopes
- [Play Framework]Body parsers——请求主体解析器
- Play Framework2之部署--项目打包成war包
- play framework2开发(二)
- play framework2开发(三)
- play framework2开发(四)
- play framework2开发(五)
- play framework2开发(六)
- play framework2开发(七)
- play framework2开发(八)
- AWS java+play framework2.1
- Play framework Body分析器
- play framework2.0.*打war包
- 网关服务器 .
- gcc 优化选项 -O1 -O2 -O3 -Os 优先级,-fomit-frame-pointer
- 使用非递归与递归来判断回文
- 闪客工具 收集一下网上有意思的方法
- Linux 进程互斥量
- play framework2学习之旅<2.5>Body parsers
- 精妙绝伦的jQuery——改进导航:菜单、标签及折叠选项
- [Unresolved Problems]
- 一个简单的游戏服务器框架 .
- 《望岳》赏析
- 《绝句》赏析
- 涂料化工行业:ORACLE EBS R12的解决方案经验总结(OPM项目)
- 《锄禾》赏析
- IBM DB2 日常维护汇总 【监控】