REST-style request format

1. What is the rest

Know the result of these two points is enough:

1.1 unified interface

In fact, rest is HTTP-based, four ways.

RESTful architecture predetermined style, metadata operations, i.e. CRUD (create, read, update and delete, i.e. deletions change check data) operation, respectively, corresponding to the HTTP method: GET to obtain resources, POST for new resources (which may used to update the resource), PUT for updating resources, dELETE to delete the resource, so that a unified interface to data manipulation, only through the HTTP method, you can complete investigation of all additions and deletions to change the working of the data.

which is:

    • GET (SELECT): Remove the resource from the server (one or more).
    • POST (CREATE): a new resource on the server.
    • PUT (UPDATE): updated resources on the server (client provides full resource data).
    • PATCH (UPDATE): In the server updates the resource (client provides the resources you need to modify data).
    • DELETE (DELETE): Delete the resource from the server.

    1.2 URI

And each particular resource corresponding to the URI, i.e. resource identifiers.

Can point to a resource with a URI (uniform resource locator), i.e. each URI corresponds to a particular resource. To obtain this resource, you can access its URI, URI therefore become a resource for each address or identifier.

In general, each resource has at least one corresponding URI, i.e., the most typical URI URL.

 1.3 distinguish between URI and URL

The need for more understanding of the HTTP protocol
URL format: protocol: // domain / path query #HASH, a real HTTP request will also include Header (including Cookie, Method, etc.)?

URI format of the resource: protocol: // domain / path, it is only a subset of the URL, characterizing a resource entity. For example, .

 

 

 

2. Sample

Request a "resource" wording

 

The front jsp:

<%

              pageContext.setAttribute("ctp", request.getContextPath());

%>

Item path ctp / order request from the name just emp / $ {id}

Back-end Controller:

@RequestMapping(value = "/emp/{id}", method = RequestMethod.GET)

    public String getEmp(@PathVariable("id") Integer id, Model model) {…}

Or do not write the same requests between front-end input in the address bar:

localhost: 8080 / emp / 1 can check out the No. 1 employee

3. four ways

 

 

But by default only get / post in two ways, how to configure two other way?

Initiated PUT, DELETE form from page requests? Spring provides support for Rest style

1), <-! Support Rest conversion style filter -> web.xml to configure the filter;

      

 <filter>

              <filter-name>HiddenHttpMethodFilter</filter-name>

              <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>

       </filter>

       <filter-mapping>

              <filter-name>HiddenHttpMethodFilter</filter-name>

              <url-pattern>/*</url-pattern>

       </filter-mapping>

 

2) How to send other forms request?

The following requirements;

1, create a post type of form

2, a single table carries parameters of _method

3, the value _method is DELETE, PUT

Example:

<form action="book/1" method="post">

     <input name="_method" value="put"/>

     <input type="submit" value="更新1号图书"/>

</form>

<form action="book/1" method="post">

     <input name="_method" value="put"/>

     <input type="submit">form<//>= "Update Books No. 1"value

 

Guess you like

Origin www.cnblogs.com/yanl55555/p/11745219.html