使用 JAX-RS 覆盖 HTTP 方法

发布于 2024-09-01 16:29:46 字数 264 浏览 10 评论 0原文

今天的浏览器(或 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不知所踪 2024-09-08 16:29:46

严格来说,不是 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.

与往事干杯 2024-09-08 16:29:46

Jersey 也有类似的东西,不过过滤了标头字段。文档可以在 此处找到

Jersey also has something similar, filtering a header field though. Documentation can be found here

小瓶盖 2024-09-08 16:29:46

引用罗伊·菲尔丁的话:

无论如何,有很多
RESTful 服务什么也不做
获取和发布
。关键是那些如何
使用方法,而不是方法是什么
用过的。举个具体的例子,什么
让 wiki 或多或少变得 RESTful 是
几乎总是由它如何决定
(ab)使用 GET 和会话——是否
它使用 PUT 或 POST 来编辑页面
相比之下是糖配料。

重点是我的,摘自这篇文章
http://tech.groups.yahoo.com/group/rest-讨论/消息/10746

To quote Roy Fielding:

In any case, there are plenty of
RESTful services that do nothing but
GET and POST
. The key is how those
methods are used, not what methods are
used. To give a specific example, what
makes a wiki more or less RESTful is
almost always determined by how it
(ab)uses GET and sessions -- whether
it uses PUT or POST for editing pages
is sugar topping in comparison.

Emphasis is mine, taken from this post
http://tech.groups.yahoo.com/group/rest-discuss/message/10746

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文