任何简单的 Java REST 教程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/333690/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:33:26  来源:igfitidea点击:

Any easy REST tutorials for Java?

javarest

提问by Vidar

Every tutorial or explanation of REST just goes too complicated too quickly - the learning curve rises so fast after the initial explanation of CRUD and the supposed simplicity over SOAP. Why can't people write decent tutorials anymore!

REST 的每个教程或解释都变得太复杂太快 - 在对 CRUD 的初步解释和假定的简单性超过 SOAP 之后,学习曲线上升得如此之快。为什么人们不能再写出像样的教程了!

I'm looking at Restlet - and its not the best, there are things missing in the tutorial and the language/grammar is a bit confusing and unclear. It has took me hours to untangle their First Steps tutorial (with the help of another Java programmer!)

我在看 Restlet - 它不是最好的,教程中缺少一些东西,语言/语法有点混乱和不清楚。我花了几个小时来解开他们的第一步教程(在另一个 Java 程序员的帮助下!)

RESTlet Tutorial Comments

RESTlet 教程评论

Overall I'm not sure exactly who the tutorial was aimed at - because there is a fair degree of assumed knowledge all round, so coming into REST and Restlet framework cold leaves you with a lot of 'catchup work' to do, and re-reading paragraphs over and over again.

总的来说,我不确定本教程的确切目标是谁 - 因为有相当程度的假设知识,所以进入 REST 和 Restlet 框架会让你有很多“追赶工作”要做,然后重新 -一遍又一遍地阅读段落。

  1. We had difficulty working out that the jars had to be in copied into the correct lib folder.

  2. Problems with web.xml creating a HTTP Status 500 error -

  1. 我们很难确定必须将 jar 复制到正确的 lib 文件夹中。

  2. web.xml 创建 HTTP 状态 500 错误的问题 -

The server encountered an internal error () that prevented it from fulfilling this request

服务器遇到内部错误 () 阻止它完成此请求

, the tutorial says:

,教程说:

"Create a new Servlet Web application as usual, add a "com.firstStepsServlet" package and put the resource and application classes in."

“像往常一样创建一个新的Servlet Web应用程序,添加一个“com.firstStepsServlet”包并将资源和应用程序类放入。”

This means that your fully qualified name for your class FirstStepsApplicationis com.firstStepsServlet.FirstStepsApplication, so we had to alter web.xml to refer to the correct class e.g:

这意味着您的类FirstStepsApplication 的完全限定名称是com.firstStepsServlet.FirstStepsApplication,因此我们必须更改 web.xml 以引用正确的类,例如:

original:

原来的:

<param-value>
         firstStepsServlet.FirstStepsApplication
</param-value>

should be:

应该:

<param-value>
         com.firstStepsServlet.FirstStepsApplication
</param-value>


Conclusion

结论

I was under the impression that the concepts of REST were supposed to be much simpler than SOAP - but it seems just as bad if not more complicated - don't get it at all! grrrr

我的印象是 REST 的概念应该比 SOAP 简单得多——但如果不是更复杂,它似乎也一样糟糕——根本不明白!咕噜噜

Any good links - much appreciated.

任何好的链接 - 非常感谢。

采纳答案by gregnostic

It sounds like you could use a solid understanding of the fundamentals of REST, and for that I highlyrecommend RESTful Web Servicesby Leonard Richardson and Sam Ruby. I provides a great introduction to REST: what it is and how to implement a (practical) RESTful web service.

听起来您可以充分了解 REST 的基础知识,为此我强烈推荐Leonard Richardson 和 Sam Ruby 的RESTful Web 服务。我对 REST 进行了精彩的介绍:它是什么以及如何实现(实用的)RESTful Web 服务。

Most of the example code in the book is actually Ruby, but it's easy enough to understand even if you're not a Ruby expert. But one thing that should help you specifically is that one of the later chapters of the book contains overviews of several RESTful frameworks, including Restlet. It doesn't really get into any code (it's a 50,000-foot flyover) but I think it'll give you just what you need at this stage.

本书中的大部分示例代码实际上是 Ruby,但即使您不是 Ruby 专家,也很容易理解。但是,应该特别帮助您的一件事是,本书后面的章节之一包含几个 RESTful 框架的概述,包括 Restlet。它并没有真正涉及任何代码(这是一个 50,000 英尺的天桥),但我认为它会给你在这个阶段你需要的东西。

回答by Jerome Louvel

Could you describe precisely what caused you troubles in our Restlet tutorials? We are interested in fixing/improving what needs to.

您能否在我们的 Restlet 教程中准确描述是什么导致了您的问题?我们有兴趣修复/改进需要的东西。

Did you check the screencasts? http://www.restlet.org/documentation/1.1/screencast/

你检查过截屏吗? http://www.restlet.org/documentation/1.1/screencast/

Otherwise, there is a Restlet tutorial in the O'Reilly book that we wrote in their Chapter 12.

否则,我们在他们的第 12 章中写的 O'Reilly 书中有一个 Restlet 教程。

If you still have troubles, please contact our mailing list: http://www.restlet.org/community/lists

如果您仍有问题,请联系我们的邮件列表:http: //www.restlet.org/community/lists

Best regards, Jér?me Louvel

最好的问候, Jér?me Louvel

Restlet ~ Founder and Lead developer ~ http://www.restlet.orgNoelios Technologies ~ Co-founder ~ http://www.noelios.com

Restlet ~ 创始人和首席开发人员 ~ http://www.restlet.orgNoelios Technologies ~ 联合创始人 ~ http://www.noelios.com

回答by Jerome Louvel

you point to an error inside the web.xml, but I've just checked in both Restlet 1.0 and 1.1 documentation, and I don't see this kind of error. I would like to mention that you can find at the end of the page a zip that contains a complete war file and the list of source files in case you want to run in "standalone" mode. Did you try the standalone mode? As explained in the tuto, you only need 2 jars in the classpath, and the source files.

你指出了 web.xml 中的一个错误,但我刚刚检查了 Restlet 1.0 和 1.1 文档,我没有看到这种错误。我想提一下,您可以在页面末尾找到一个包含完整战争文件和源文件列表的 zip,以防您想以“独立”模式运行。你试过单机模式吗?如教程中所述,您只需要类路径中的 2 个 jar 和源文件。

We would like also to point that the "firstSteps" tutorial is not a tutorial on REST but on the programmation with Restlet. You can also have a look at the "firstResource" tutorial (http://www.restlet.org/documentation/1.1/firstResource) which describes how to develop a simple resource and which introduces some concepts of the REST architecture style. It should help you.

我们还想指出,“firstSteps”教程不是关于 REST 的教程,而是关于使用 Restlet 进行编程的教程。您还可以查看“firstResource”教程 ( http://www.restlet.org/documentation/1.1/firstResource),它描述了如何开发一个简单的资源并介绍了 REST 架构风格的一些概念。它应该可以帮助你。

回答by Dan Dyer

Not specifically Java-related, but I thought these InfoQ articles were quite good:

与 Java 无关,但我认为这些 InfoQ 文章非常好:

回答by Jerome Louvel

In addition, the qualified named suggested in the tutorial is indeed "firstStepsServlet.FirstStepsApplication" and not "com.firstStepsServlet.FirstStepsApplication".

另外,教程中建议的限定名确实是“firstStepsServlet.FirstStepsApplication”而不是“com.firstStepsServlet.FirstStepsApplication”。

Therefore, the suggested param value in web.xml is correct.

因此,web.xml 中建议的参数值是正确的。

Could you confirm that this fixes your issue? Also, did you have Servlet deployment knowledge before reading the tutorial? If not, that would explain the pain. In this case, the standalone mode is probably a better fit.

你能确认这解决了你的问题吗?另外,在阅读本教程之前,您是否具备 Servlet 部署知识?如果没有,那就可以解释疼痛了。在这种情况下,独立模式可能更合适。

回答by Hai Nguyen

eSoftHead company has just released a short tutorialof developing Restful application by using RESTeasy.

eSoftHead 公司刚刚发布了一个使用 RESTeasy 开发 Restful 应用程序的简短教程

回答by PIXAR

The Restlet framework is composed of four main parts.

Restlet 框架由四个主要部分组成。

1. Restlet API

1. Restlet API

First, there is the "Restlet API", a neutral API supporting the concepts of REST and facilitating the handling of calls for both client-side and server-side applications. This API is backed by the Restlet Engine and both are now shipped in a single JAR ("org.restlet.jar").

首先是“Restlet API”,这是一个中立的 API,支持 REST 的概念并促进处理客户端和服务器端应用程序的调用。此 API 由 Restlet 引擎支持,现在两者都在单个 JAR(“ org.restlet.jar”)中提供。

This separation between the API and the implementation is similar to the one between the Servlet API and Web containers like Jetty or Tomcat, or between the JDBC API and concrete JDBC drivers.

API 和实现之间的这种分离类似于 Servlet API 和 Web 容器(如 Jetty 或 Tomcat)之间的分离,或者 JDBC API 和具体的 JDBC 驱动程序之间的分离。

2. Retrieving the content of a Web page

2. 检索网页内容

As we mentioned in the introduction paper, the Restlet framework is at the same time a client and a server framework. For example, Restlet can easily work with remote resources using its HTTP client connector. A connector in REST is a software element that enables the communication between components, typically by implementing one side of a network protocol. Restlet provides several implementations of client connectors based on existing open-source projects. The connectors section lists all available client and server connectors and explain how to use and configure them.

正如我们在介绍论文中提到的,Restlet 框架同时是一个客户端和一个服务器框架。例如,Restlet 可以使用其 HTTP 客户端连接器轻松处理远程资源。REST 中的连接器是一种软件元素,它支持组件之间的通信,通常通过实现网络协议的一侧来实现。Restlet 提供了几种基于现有开源项目的客户端连接器实现。连接器部分列出了所有可用的客户端和服务器连接器,并解释了如何使用和配置它们。

Here we will get the representation of an existing resource and output it in the JVM console:

在这里,我们将获取现有资源的表示并将其输出到 JVM 控制台中:

// Outputting the content of a Web page  
new ClientResource("http://").get().write(System.out);  

Note that the example above uses a simplified way to issue calls via the ClientResourceclass. If you need multi-threading or more control it is still possible to manipulate use the Clientconnector class or the Requestobjects directly. The example below how to set some preferences in your client call, like a referrer URI. It could also be the languages and media types you prefer to receive as a response:

请注意,上面的示例使用一种简化的方式通过ClientResource类发出调用。如果您需要多线程或更多控制,仍然可以直接使用Client连接器类或Request对象进行操作。下面的示例如何在您的客户端调用中设置一些首选项,例如引用 URI。它也可能是您希望收到的回复的语言和媒体类型:

// Create the client resource  
ClientResource resource = new ClientResource("http://www.restlet.org");  

// Customize the referrer property  
resource.setReferrerRef("http://www.mysite.org");  

// Write the response entity on the console  
resource.get().write(System.out);  

3. Listening to Web browsers

3. 收听网络浏览器

Now, we want to see how the Restlet framework can listen to client requests and reply to them. We will use the internal Restlet HTTP server connector (even though it is possible to switch to others such as the one based on Mortbay's Jetty) and return a simple string representation "hello, world" as plain text. Note that the Part03class extends the base ServerResourceclass provided by Restlet:

现在,我们想看看 Restlet 框架如何监听客户端请求并回复它们。我们将使用内部 Restlet HTTP 服务器连接器(即使可以切换到其他连接器,例如基于 Mortbay 的 Jetty 的连接器)并以纯文本形式返回一个简单的字符串表示“hello, world”。注意Part03类扩展了Restlet 提供的基本ServerResource类:

 public class Part03 extends ServerResource {  

    public static void main(String[] args) throws Exception {  
        // Create the HTTP server and listen on port 8182  
        new Server(Protocol.HTTP, 8182, Part03.class).start();  
    }  

    @Get  
    public String toString() {  
        return "hello, world";  
    }  

}  

If you run this code and launch your server, you can open a Web browser and hit the . Actually, any URI will work, try also. Note that if you test your server from a different machine, you need to replace "localhost" by either the IP address of your server or its domain name if it has one defined.

如果您运行此代码并启动您的服务器,您可以打开 Web 浏览器并点击 . 实际上,任何 URI 都可以使用,也可以尝试。请注意,如果您在另一台机器上测试您的服务器,则需要将“localhost”替换为您的服务器的 IP 地址或其域名(如果已定义)。

So far, we have mostly showed you the highest level of abstraction in the Restlet API, with the ClientResource and ServerResource classes. But as we move forward, you will discover that those two classes are supported by a rich API, letting you manipulate all the REST artifacts.

到目前为止,我们主要向您展示了 Restlet API 中最高级别的抽象,包括 ClientResource 和 ServerResource 类。但是随着我们继续前进,您会发现这两个类得到了丰富的 API 的支持,让您可以操作所有 REST 工件。

4. Overview of a REST architecture

4. REST 架构概述

Let's step back a little and consider typical web architectures from a REST point of view. In the diagram below, ports represent the connector that enables the communication between components which are represented by the larger boxes.

让我们退后一步,从 REST 的角度考虑典型的 Web 架构。在下图中,端口代表连接器,该连接器支持由较大框表示的组件之间的通信。

回答by Sunil Rodrigues

Here is a well written tutorial on REST.. http://rest.elkstein.org/2008/02/what-is-rest.html

这是一个写得很好的 REST 教程.. http://rest.elkstein.org/2008/02/what-is-rest.html