java 使用 Flex 创建 REST 客户端是否可行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/153420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it feasible to create a REST client with Flex?
提问by Alotor
I'm starting a project using a Restful architecture implemented in Java (using the new JAX-RS standard)
我正在使用在 Java 中实现的 Restful 架构开始一个项目(使用新的 JAX-RS 标准)
We are planning to develop the GUI with a Flex application. I have already found some problems with this implementation using the HTTPService component (the response error codes, headers access...).
我们计划使用 Flex 应用程序开发 GUI。我已经发现使用 HTTPService 组件的这个实现存在一些问题(响应错误代码、标头访问......)。
Any of you guys have some experience in a similar project. Is it feasible?
你们中的任何人都有类似项目的经验。可行吗?
采纳答案by Guerry
The problem here is that a lot of the web discussions around this issue are a year or more old. I'm working through this same research right now, and this is what I've learned today.
这里的问题是很多围绕这个问题的网络讨论都已经有一年或更长时间了。我现在正在做同样的研究,这就是我今天学到的。
This IBM Developer Works article from August 2008by Jorge Rasillo and Mike Burr shows how to do a Flex front-end / RESTful back-end app (examples in PHP and Groovy). Nice article. Anyway, here's the take away:
Jorge Rasillo 和 Mike Burr于 2008 年 8 月发表的这篇IBM Developer Works 文章展示了如何制作 Flex 前端/RESTful 后端应用程序(PHP 和 Groovy 中的示例)。不错的文章。无论如何,这是要点:
- Their PHP/Groovy code uses and expectsPUT and DELETE.
- But the Flex code has to use POST, but sets the HTTP header X-Method-Override to DELETE (you can do the same for PUT I presume).
- Note that this is notthe Proxy method discussed above.
- 他们的 PHP/Groovy 代码使用并期望PUT 和 DELETE。
- 但是 Flex 代码必须使用 POST,但将 HTTP 标头 X-Method-Override 设置为 DELETE(我认为您可以对 PUT 执行相同操作)。
- 请注意,这不是上面讨论的 Proxy 方法。
// Flex doesn't know how to generate an HTTP DELETE.
// Fortunately, sMash/Zero will interpret an HTTP POST with
// an X-Method-Override: DELETE header as a DELETE.
deleteTodoHS.headers['X-Method-Override'] = 'DELETE';What's happening here? the IBM web server intercepts and interprets the "POST with DELETE" as a DELETE.
这里发生了什么事?IBM Web 服务器拦截并将“POST with DELETE”解释为 DELETE。
So, I dug further and found this post and discussion with Don Box(one of the original SOAP guys). Apparently this is a fairly standard behavior since some browsers, etc. do not support PUT and DELETE, and is a work-around that has been around a while. Here's a snippet, but there's much more discussion.
所以,我进一步挖掘并找到了这篇文章并与 Don Box(最初的 SOAP 人之一)进行了讨论。显然,这是一个相当标准的行为,因为某些浏览器等不支持 PUT 和 DELETE,并且是一种已经存在一段时间的变通方法。这是一个片段,但还有更多的讨论。
"If I were building a GData client, I honestly wonder why I'd bother using DELETE and PUT methods at all given that X-HTTP-Method-Override is going to work in more cases/deployments."
“如果我正在构建一个 GData 客户端,老实说,我想知道为什么我要费心使用 DELETE 和 PUT 方法,因为 X-HTTP-Method-Override 将在更多案例/部署中工作。”
My take away from this is that if your web side supports this X-Method-Override header, then you can use this approach. The Don Box comments make me think it's fairly well supported, but I've not confirmed that yet.
我从中得出的结论是,如果您的 Web 端支持此 X-Method-Override 标头,那么您可以使用此方法。Don Box 的评论让我认为它得到了很好的支持,但我还没有确认。
Another issue arises around being able to read the HTTP response headers. Again, from a blog post in 2007 by Nathan de Vries, we see this discussed. He followed up that blog post and discussion with his own comment:
另一个问题是关于能够读取 HTTP 响应标头。同样,从Nathan de Vries 2007 年的一篇博客文章中,我们看到了这一点。他用自己的评论跟进了那篇博文和讨论:
"The only change on the web front is that newer versions of the Flash Player (certainly those supplied with the Flex 3 beta) now support the responseHeaders property on instances of HTTPStatusEvent."
“Web 方面的唯一变化是较新版本的 Flash Player(当然是随 Flex 3 测试版提供的版本)现在支持 HTTPStatusEvent 实例的 responseHeaders 属性。”
I'm hoping that means it is a non-issue now.
我希望这意味着它现在不是问题。
回答by Theo
As many have pointed out HTTPServiceis a bit simplistic and doesn't do all that you want to do. However, HTTPServiceis just sugar on top of the flash.net.*classes like URLLoader, URLRequestand URLRequestHeader. Using these you can assemble most HTTP requests.
正如许多人指出的那样HTTPService有点简单,并没有做你想做的所有事情。但是,HTTPService这只是flash.net.*诸如URLLoader,URLRequest和之类的类之上的糖URLRequestHeader。使用这些可以组合大多数 HTTP 请求。
When it comes to support for other methods than GET and POST the problem mostly lies in that some browsers (for example Safari) don't support these, and Flash Player relies on the browser for all it's networking.
当谈到支持 GET 和 POST 之外的其他方法时,问题主要在于某些浏览器(例如 Safari)不支持这些方法,而 Flash Player 的所有网络连接都依赖于浏览器。
回答by mbrevoort
There are definite shortcomings of Flex's ability to act as a pure RESTful client.
Flex 作为纯 RESTful 客户端的能力存在明显的缺陷。
The comments below are from this blog:
下面的评论来自这个博客:
The problem is HTTPService class has several major limitations:
- Only GET and POST methods are supported out of the box (unless you use FDS and set useProxy attribute to true)
- Not able to set request headers and there is no access to response headers. Therefore I am not able to access the response body in the case of an error.
- It HTTPService gets a status code anything other 200, it consider an error. (event 201, ouch!!). The FaultEvent doesn't provide information about the status code any response body. The Flex client will have no idea what went wrong.
问题是 HTTPService 类有几个主要限制:
- 开箱即用仅支持 GET 和 POST 方法(除非您使用 FDS 并将 useProxy 属性设置为 true)
- 无法设置请求标头,也无法访问响应标头。因此,我无法在出现错误的情况下访问响应正文。
- 如果 HTTPService 获取状态代码为 200 以外的任何内容,则认为是错误。(事件 201,哎哟!!)。FaultEvent 不提供有关任何响应正文的状态代码的信息。Flex 客户端将不知道出了什么问题。
Matt Raiblealso gave a nice presentation on REST with Rails, Grails, GWT and Flexthat have some good references linked from it.
Matt Raible还就 REST with Rails、Grails、GWT 和 Flex进行了精彩的演示,其中有一些很好的参考资料。
Whether it's feasible or not really depends on how much your willing to work around by proxying, etc.
它是否可行实际上取决于您愿意通过代理等解决问题的程度。
回答by mbrevoort
I've been working on an open source replacement for the HTTPService component that fully supports REST. If interested, you can find the beta version (source code and/or compiled Flex shared runtime library) and instructions here:
我一直在研究完全支持 REST 的 HTTPService 组件的开源替代品。如果有兴趣,您可以在此处找到测试版(源代码和/或编译的 Flex 共享运行时库)和说明:
回答by verveguy
The short answer is yes, you can do RESTful with Flex. You just have to work around the limitations of the Flash player (better with latest versions) and the containing browser's HTTP stack limitations.
简短的回答是肯定的,您可以使用 Flex 实现 RESTful。您只需要解决 Flash 播放器的限制(最好使用最新版本)和包含浏览器的 HTTP 堆栈限制。
We've been doing RESTful client development in Flex for more than a year after solving the basic HTTP request header and lack of PUT and DELETE via the rails-esque ?_method= approach. Tacky perhaps, but it gets the job done.
在通过 rails-esque ?_method= 方法解决基本的 HTTP 请求标头和缺少 PUT 和 DELETE 之后,我们已经在 Flex 中进行 RESTful 客户端开发一年多。也许很俗气,但它可以完成工作。
I noted some of the headers pain in an old blog post at http://verveguy.blogspot.com/2008/07/truth-about-flex-httpservice.html
我在http://verveguy.blogspot.com/2008/07/truth-about-flex-httpservice.html 上的一篇旧博客文章中注意到了一些标题的痛苦
回答by Phil
Flex support for REST is weak at best. I spent a lot of time building a prototype so I know most of the issues. As mentioned previously , out of the box there is only support for GET and POST. At first glance it appears that you can use the proxy config in LiveCycle Data Services or Blaze to get support for PUT and DELETE. However, its a sham. The request coming from your Flex app will still be a POST. The proxy converts it to PUT or DELETE on the server side to trick your server side code. There are other issues as well. It's heard to believe that this is the best that Adobe could come up with. After my evaluation we decided to go in another direction.
Flex 对 REST 的支持充其量是微弱的。我花了很多时间构建原型,所以我知道大部分问题。如前所述,开箱即用仅支持 GET 和 POST。乍一看,您似乎可以使用 LiveCycle Data Services 或 Blaze 中的代理配置来获得对 PUT 和 DELETE 的支持。然而,这是一个骗局。来自 Flex 应用程序的请求仍将是 POST。代理在服务器端将其转换为 PUT 或 DELETE 以欺骗您的服务器端代码。还有其他问题。听说这是 Adobe 能想到的最好的方法。经过我的评估,我们决定朝另一个方向发展。
回答by Lance Pollard
RestfulXhas solved most/all of the REST problems with Flex. It has support for Rails/GAE/Merb/CouchDB/AIR/WebKit, and I'm sure it would be a snap to connect it to your Java implementation.
RestfulX已经用 Flex 解决了大部分/所有 REST 问题。它支持 Rails/GAE/Merb/CouchDB/AIR/WebKit,我相信将它连接到您的 Java 实现会很容易。
Dima's integrated the AS3HTTPClient Library into it also.
Dima 也将 AS3HTTPClient 库集成到其中。
Check it out!
看看这个!
回答by Brandon
Yes, I was able to use POST and access headers with this component:
是的,我能够使用 POST 和访问该组件的标头:
http://code.google.com/p/as3httpclient/wiki/Links
回答by dj_segfault
I'm working right now on an application that relies heavily on REST calls between Flex and JavaScript and Java Servlets. We get around the response error code problem by establishing a convention of a <status id="XXX" name="YYYYYY"> block that gets returned upon error, with error IDs that roughly map to HTTP error codes.
我现在正在开发一个严重依赖 Flex 与 JavaScript 和 Java Servlet 之间的 REST 调用的应用程序。我们通过建立一个 <status id="XXX" name="YYYYYY"> 块的约定来解决响应错误代码问题,该块在错误时返回,错误 ID 大致映射到 HTTP 错误代码。
We get around the cross-site scripting limitations by using a Java Servlet as an HTTP proxy. Calls to the proxy (which runs on the same server that serves the rest of the content, including the Flex content, sends the request to the other server, then sends the response back to the original caller.
我们通过使用 Java Servlet 作为 HTTP 代理来绕过跨站点脚本限制。调用代理(运行在提供其余内容(包括 Flex 内容)的同一服务器上,将请求发送到另一台服务器,然后将响应发送回原始调用者。
回答by Yaba
Actually were are already using Flex with a Rest-Style Framework. As mbrevort already mentioned PUT and DELETE methods cannot be directly used. Instead we are doing PUT via a POST and for DELETE we are using a GET on a resource with an URL parameter like ?action=delete.
实际上已经在使用带有 Rest-Style Framework 的 Flex。正如 mbrevort 已经提到的,不能直接使用 PUT 和 DELETE 方法。相反,我们通过 POST 执行 PUT,而对于 DELETE,我们在资源上使用 GET,该资源具有诸如 ?action=delete 之类的 URL 参数。
This is not 100% Rest style, so I am not sure, if this works with a JSR 311 implementation. You will need some flexbility on the server side to workaround the PUT and DELETE restrictions.
这不是 100% Rest 风格,所以我不确定这是否适用于 JSR 311 实现。您需要在服务器端有一些灵活性来解决 PUT 和 DELETE 限制。
With regards to error handling, we have implemented an error service. In case of an server side error, the Flex application can query this error service to get the actual error message. This is also much more flexible than just mapping HTTP return codes to static messages.
关于错误处理,我们实现了错误服务。如果出现服务器端错误,Flex 应用程序可以查询此错误服务以获取实际错误消息。这也比仅仅将 HTTP 返回码映射到静态消息灵活得多。
However thanks To ECMA scripting of Flex working with XML based REST services is very easy.
然而,多亏了 Flex 的 ECMA 脚本,使用基于 XML 的 REST 服务非常容易。

