Restful interface: software architecture style, restful Architecture
Restful APIs are a software architecture style. The design style is not standard, but provides a set of design principles and constraints. It is mainly used for client-to-server interaction software. The software based on this style can be simpler, more hierarchical, and easier to implement caching and other mechanisms.
In the Restful style, the url requested by the user uses the same url and the request method: get, post, delete, put... in this way, the front-end developers will not confuse the requested resource addresses or check a lot of method names in the separate development of the front and back ends, form a unified interface.
In the Restful style, the existing rules are as follows:
GET (SELECT): queries from the server. You can use the Request Parameters on the server to differentiate the query methods. POST (CREATE): CREATE a resource on the server and call the insert operation. PUT (UPDATE): update resources on the server and call the UPDATE operation. UPDATE: UPDATE resources on the server (the client provides the changed attributes ). (Currently, JDK 7 is not implemented, and tomcat 7 is not ). DELETE: deletes resources from the server and calls the DELETE statement.
This defined specification can be called a restful API interface. We can implement various operations through the same url.
Next we will explain how to implement restful APIs in spring-mvc and solve the problems! (Java web does not support put and delete requests)
First, we set up the spring mvc project interface and write the Controller in a restful style. Here I wrote a User controller class and a User "Action"->
Here, the url addresses of the controller and action are the access addresses written in the restful style./The User/User uses the method to differentiate the request methods.
Our front-end uses jquery ajax for requests.
Someone will ask? Why does delete and put use post requests? Here we will talk about the unsupported put and delete operations in java->
In java, the put and delete requests are filtered out (I don't know why), and the servlet also contains the corresponding doGet, doPost, doDelete, and doPut methods, but it cannot be used (embarrassing). Similarly, spring mvc also has the corresponding method = RequestMethod. PUT and Delete, but the type in ajax can be written as Put or Delete to access the corresponding method, but the parameter cannot be passed, all parameters passed in the past are null (not depressing )! C # won't be like this. C #'s API programming needs to enable PUT and Delete, and it doesn't need to be so complicated in java. Here we will solve this problem->
First, add a filter in the Web. xml file of the springMVC project.
1
2
3
HiddenHttpMethodFilter
4
Org. springframework. web. filter. HiddenHttpMethodFilter
5
6
7
HiddenHttpMethodFilter
8
/*
9
Of course, some new users do not know where to add this code, so I will paste my web. xml here (I am also doing this for a long time ...)
1
2
6 7
8
9
HiddenHttpMethodFilter
10
Org. springframework. web. filter. HiddenHttpMethodFilter
11
12
13
HiddenHttpMethodFilter
14
/*
15
16 17
18
26 27 28
29
/Index. jsp
30
31
32
33
Spring
34
Org. springframework. web. servlet. DispatcherServlet
35 36
37
1
38
39 40
41
Spring
42
/
43
44 45
46
47
Org. springframework. web. context. ContextLoaderListener
48
49 50
51
52
ContextConfigLocation
53
Classpath: applicationContext. xml
54
55
Here we have configured the filter, and I have commented out it. If you use the following configuration file->
1
2
3
HttpMethodPutFilter
4
Org. springframework. web. filter. HttpPutFormContentFilter
5
6
7
HttpMethodPutFilter
8
/*
9
If this configuration item is written here, it can support PUT requests, but the DELETE request is still unavailable, so I can only select the first method.
1
2
hiddenHttpMethodFilter
3
org.springframework.web.filter.HiddenHttpMethodFilter
4
5
6
hiddenHttpMethodFilter
7
/*
8
This section uses the built-in filter class of org. springframework. web. filter. HiddenHttpMethodFilter to standardize http requests. In this way, we can declare the request method.
After the configuration is complete, we need to pass a parameter _ method: "PUT" and _ method: "DELETE" in ajax, but the request method is still POST
With this configuration, we can access the DELETE-modified method. Similarly, we can access the PUT-modified method by using _ method: 'put, in this way, the Controller class defined above can be implemented.