使用 JAX-RS 覆盖 HTTP 方法
今天的浏览器(或 HTML < 5)仅支持 HTTP GET 和 POST,但要进行 RESTful 通信也需要 PUT 和 DELETE。如果解决方法不应该使用 Ajax,则需要使用隐藏表单字段之类的东西来覆盖实际的 HTTP 方法。
Rails 使用以下技巧:
<input name="_method" type="hidden" value="put" />
是否有可能对 JAX-RS 做类似的事情?
Today's browsers (or HTML < 5) only support HTTP GET and POST, but to communicate RESTful one need PUT and DELETE too. If the workaround should not be to use Ajax, something like a hidden form field is required to overwrite the actual HTTP method.
Rails uses the following trick:
<input name="_method" type="hidden" value="put" />
Is there a possibility to do something similar with JAX-RS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
严格来说,不是 JAX-RS 解决方案,但 spring 3.0 附带了
HiddenHttpMethodFilter
完全实现了 Rails 的技巧,默认为相同的_method
参数。请注意,您不需要使用所有 spring 来使用此过滤器,您只需在
web.xml
中配置它并忽略 spring 的其余部分。Not strictly a JAX-RS solution but spring 3.0 comes with a
HiddenHttpMethodFilter
that implements exactly the trick that rails does, defaults to the same_method
parameter.Note that you don't need to be using all of spring to use this filter, you can just configure it in your
web.xml
and ignore the rest of spring.Jersey 也有类似的东西,不过过滤了标头字段。文档可以在 此处找到
Jersey also has something similar, filtering a header field though. Documentation can be found here
引用罗伊·菲尔丁的话:
重点是我的,摘自这篇文章
http://tech.groups.yahoo.com/group/rest-讨论/消息/10746
To quote Roy Fielding:
Emphasis is mine, taken from this post
http://tech.groups.yahoo.com/group/rest-discuss/message/10746