如何将请求正文绑定到操作方法参数?
我了解 Play 如何将 URI 段或参数绑定到操作方法参数。我还看到了如何访问上传的文件。
但我仍在寻找将 PUT 或 POST 请求的请求实体绑定到方法参数的方法。
假设请求是这样的
PUT /blog/entries/67
Content-Type: application/atom+xml
<entry>...</entry>
我想将其绑定到条目参数:
public static void (String entryId, Entry entry) {
// entryId being 67
// entry being the deserialized Atom payload (e.g. using Apache Abdera parser)
backend.updateEntry(67,entry);
// AtomPub's 'canonical' response is the updated entry.
render(entry)
}
两个问题:
这样的东西有效吗?
在哪里可以找到有关如何创建解串器的文档?
I understand how Play binds URI segments or parameters to action method parameters. I also saw how an uploaded file can be accessed.
But I am still looking for the way to bind the request entity of a PUT or POST request to a method parameter.
Suppose the request is sth like
PUT /blog/entries/67
Content-Type: application/atom+xml
<entry>...</entry>
And I would like to bind that to an entry parameter:
public static void (String entryId, Entry entry) {
// entryId being 67
// entry being the deserialized Atom payload (e.g. using Apache Abdera parser)
backend.updateEntry(67,entry);
// AtomPub's 'canonical' response is the updated entry.
render(entry)
}
Two questions:
Does something like this work?
Where do I find the documentation of how to create the deserializer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 Play 网站上的自定义绑定文档。
http://www.playframework.org/documentation/1.2.3/controllers#custombinding
我认为您正在寻找的是
play.data.binding.TypeBinder
,它允许您自定义 Play 绑定控制器中某些对象的方式。更新:
查看游戏组,Guillaume 发布了以下代码,用于在 POST 正文中处理 JSON,因此可以轻松地对其进行修改以从atom+xml 输入获取 XML。它使用 BinderPlugin,而不是 TypeBinder,这允许您执行更强大的绑定操作。
Take a look at the Custom Binding documentation on the Play site.
http://www.playframework.org/documentation/1.2.3/controllers#custombinding
I think what you are looking for is
play.data.binding.TypeBinder
, which allows you to customise the way Play binds certain objects in your controller.Update:
Looking at the play groups, Guillaume has posted the following code for handling JSON in the body of a POST, so this could easily be adapted to get XML from an atom+xml input. It uses a BinderPlugin, rather than the TypeBinder, which allows you to do more pwerful binding operations.