如何在Web应用程序中实现REST? [英] How to implement REST in a web application?

查看:257
本文介绍了如何在Web应用程序中实现REST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在我的Web应用程序中实现REST。我想基于此服务创建一个Web应用程序,但我不知道该怎么做。现在,我正在使用J2EE和Tomcat。这些技术应考虑哪些事项?

I want to know how I could implement REST in my web application. I want to create a web application based on this service, but I don't know how to do it. Now, i'm using J2EE and Tomcat. What things should be considered for these technologies?

编辑:
对不起,我的意思是REST服务。

Sorry, I mean RESTful service.

推荐答案

REST不是特定的界面或技术,而是一种风格。最好的例子是网络本身 - 您的浏览器向网络服务器发送HTTP请求,网络服务器以网页响应。

REST is not specific interface or technology, but a style. The best example is the web itself - your browser sends an HTTP request to a web server, which responds with a web page.

在此上下文中的Representational State Transfer:表示是网页,状态是其中包含的信息。我们可以通过切换到提供xml而不是html来更改表示,但信息将是相同的。

Representational State Transfer in this context: The representation is the web page, the state is the information contained in it. We could change the representation by switching to serving up xml instead of html, but the information would be the same.

在RESTful服务中,您使用此样式发送数据来回对象 - 状态从服务器传输到您,然后再次发送新状态。

In a RESTful service, you use this style to send data objects back and forth - the state is transferred from the server to you, and then you send a new state back again.

所以,从某种意义上说,如果你将服务器页面作为资源,Tomcat已经为你做了REST: http://carsales.com/cars/porsche2149 可能是您汽车的资源,您可以使用HTTP POST或PUT更改其详细信息。

So, in a sense, Tomcat will already do REST for you, if you put your server pages as resources: http://carsales.com/cars/porsche2149 could be the resource for your car, to which you could use HTTP POST or PUT to change the details of it.

REST的标志是使用URI来表示资源,如上所述,使用JSON或XML作为交换媒介(尽管使用了AHAH和其他格式),并争论如何删除集合。

The hallmarks of REST are using URIs to denote resources, as above, using JSON or XML as the interchange medium (although AHAH and other formats are used), and arguing about how to DELETE collections.

首先,弄清楚你的资源是什么,并组织你的URI系统以适应它(使用URL重写等)。然后确定要使用的表示。最后,编写后端来处理传递状态表示,并更新数据库。

First, work out what your resources will be, and organise your URI system to fit it (use URL rewriting etc). Then determine the representation(s) you want to use. Finally, write the backend to deal with passing state representations around, and update the database.

这篇关于如何在Web应用程序中实现REST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆