REST和HTTP协议有什么区别? [英] What is the difference between REST and HTTP protocols?

查看:943
本文介绍了REST和HTTP协议有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是REST协议?它与HTTP协议的区别是什么?

What is the REST protocol and what does it differ from HTTP protocol ?

推荐答案

REST是一种利用HTTP的方法协议,并不是它的替代品。

REST is an approach that leverages the HTTP protocol, and is not an alternative to it.

  • http://en.wikipedia.org/wiki/Representational_State_Transfer

数据是唯一的由URL引用,可以使用HTTP操作(GET,PUT,POST,DELETE等)进行操作。消息/响应支持各种各样的mime类型,但XML和JSON是最常见的。

Data is uniquely referenced by URL and can be acted upon using HTTP operations (GET, PUT, POST, DELETE, etc). A wide variety of mime types are supported for the message/response but XML and JSON are the most common.

例如,要读取有关客户的数据,您可以使用HTTP使用URL http://www.example.com/customers/1 进行操作。如果要删除该客户,只需使用具有相同URL的HTTP删除操作。

For example to read data about a customer you could use an HTTP get operation with the URL http://www.example.com/customers/1. If you want to delete that customer, simply use the HTTP delete operation with the same URL.

下面的Java代码演示了如何通过HTTP协议进行REST调用:

The Java code below demonstrates how to make a REST call over the HTTP protocol:

String uri =
    "http://www.example.com/customers/1";
URL url = new URL(uri);
HttpURLConnection connection =
    (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");

JAXBContext jc = JAXBContext.newInstance(Customer.class);
InputStream xml = connection.getInputStream();
Customer customer =
    (Customer) jc.createUnmarshaller().unmarshal(xml);

connection.disconnect();

对于Java(JAX-RS)示例,请参阅:

For a Java (JAX-RS) example see:

  • http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-45.html

这篇关于REST和HTTP协议有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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