What is the difference between REST and HTTP protocols?

前端 未结 5 766
猫巷女王i
猫巷女王i 2021-01-30 04:27

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

5条回答
  •  孤城傲影
    2021-01-30 04:49

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

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

    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.

    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.

    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();
    

    For a Java (JAX-RS) example see:

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

提交回复
热议问题