是否可以使用 JAX-RS 作为 Web 框架?
我最近一直在研究 JAX-RS,因为我真的很喜欢 Java 平台和 RESTful 风格的 Web 开发。我已经阅读了很多有关使用 JAX-RS 创建 RESTful Web 服务的内容,但是是否可以使用 JAX-RS 创建 RESTful 网站?我想更具体地说,是否可以使用 JAX-RS 作为控制器(从服务器检索所需的数据),然后将控制转发到视图引擎以呈现适当的 HTML?
我一直在谷歌上搜索,但没有找到任何资源来告诉你如何做到这一点。
感谢您的任何见解/帮助。
I've been looking into JAX-RS lately because I really like the Java platform and a RESTful style of web development. I've read a lot about using JAX-RS for creating RESTful Web Services, but is it possible to use JAX-RS to create RESTful web sites? I guess more specifically, is it possible to use JAX-RS as a controller (to retrieve required data from the server) and then forward control to a view engine to render the appropriate HTML?
I've been googling around but haven't found any resources that show you how to do this.
Thanks for any insight/help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实只想仅依赖 JAX-RS 作为您的 Web 框架,Jersey 可能是您最好的选择。请记住,您从中获得的功能将是最低限度的,并且您显然不会获得 JSF、Wicket 等提供的所有花哨功能。
如果您知道您的 Web 应用程序将依赖于 Spring ,也许你应该考虑使用Spring MVC 3.0。它提供了类似的宁静的 Web 服务,并且为您提供了更好的功能,因此您不需要自己实现其中的大部分功能。诚然,Spring MVC 3.0 不是 JAX-RS 的实现,并且根据 Spring 开发人员的说法,他们似乎永远不会将 Spring MVC 作为 JAX-RS 的实现,因为它们已经是相当多的稳定实现了。然而,在我看来,语法非常相似,或者至少我能够很快地理解它们,即使我已经使用 Jersey 有一段时间了。
If you truly want to rely just only JAX-RS for your web framework, Jersey might be your best bet. Keep in mind the features you get from it are going to be bare minimum and you are obviously not going to get all the bells and whistles like what's provided by JSF, Wicket, etc.
If you know your web application is going to rely on Spring, perhaps you should consider using Spring MVC 3.0. It provides restful web services-alike and it gives you better features so that you don't need to implement most of them yourself. Granted, Spring MVC 3.0 is not an implementation of JAX-RS and based on what the Spring developer said, it seems like they will never make Spring MVC as an implementation of JAX-RS since they are already quite a few stable implementations out there. However, the syntax is pretty similar in my opinion, or at least I was able to understand them rather quickly even though I have been using Jersey for quite awhile.
德伯恩是对的,几乎没有人这样做。更传统的做法是使用 JAX-RS 将信息转储为 JSON 或 XML。然后你喜欢带有 RIA 框架的 Web 浏览器(例如 Ext JS),它处理操作 DOM 并注入以 JSON/XML 形式获取的数据。这个方法很强大。您可以为该服务编写多个(可能是非浏览器)客户端,所有客户端都解析相同的 JSON/XML。您可以编写“单页”Web 应用程序,其中所有信息交换在初始页面加载后通过 AJAX 进行。我敦促您根据您的特定问题进行调查并考虑其优点和缺点。
回到你的问题:答案是“有点”。 JAX-RS 规范(从 1.1 开始)不直接提供此功能。然而,它位于 JAX-RS 参考实现 Jersey 中,通过
Viewable
响应对象。如果您想进一步调查,请参阅此博客文章:http://blogs.oracle.com/sandoz /entry/mvcj。我想指出的是,我对泽西岛的这一面没有经验。使用 Jersey 编写返回 XML/JSON 的 Web 服务是一件令人愉快的事情,但我无法谈论服务器端 HTML 模板业务。编辑:dbyrne 编辑了他的答案,以包含一篇指向上述内容的博客文章。我认为我们都得出了大致相同的答案。
dbyrne is right that almost no one is doing this. It's more conventional to use JAX-RS to dump information to JSON or XML. Then you fancy up the web browser with an RIA framework (e.g. Ext JS), which handles manipulating the DOM and injecting data as its fetched in JSON/XML form. This approach is powerful. You can write multiple, possibly non-browser clients for the service, all parsing the same JSON/XML. You can write "one-page" webapps, where all information exchange happens through AJAX after the initial pageload. I urge you to investigate and consider its strengths and weaknesses in the context of your particular problem.
Returning to your question: the answer is "sort of". This functionality is not directly provided by the JAX-RS spec (as of 1.1). However, it is in the JAX-RS reference implementation, Jersey, through the
Viewable
response object. See this blog post if you want to investigate further: http://blogs.oracle.com/sandoz/entry/mvcj. I want to point out that I have no experience with this side of Jersey. It has been pleasant to write XML/JSON-returning web services with Jersey, but I can't speak to this server-side HTML templating business.Edit: dbyrne's edited his answer to include a blog post which points to the one mentioned above. I think we've both converged on approximately the same answer.
我认为您在谷歌搜索方面遇到困难,因为没有多少人这样做。 JAX-RS 是为 Web 服务而设计的,因此开发人员不会考虑将其用作 Web 应用程序的控制器。然而,没有理由它不起作用。
查看此博客文章: JAX-RS 作为一个 Java Web 框架来统治它们?
我认为这正是您正在寻找的东西。
I think you are having difficulty with your google searches because not many people are doing this. JAX-RS was designed for web services, so developers don't think of using it as a controller for web applications. However, there is no reason it wouldn't work.
Check out this blog post: JAX-RS as the one Java web framework to rule them all?
I think it's exactly the kind of thing you are looking for.