SpringMVC for Restful API interface development

Source: Internet
Author: User

SpringMVC for Restful API interface development

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.

After learning about the style definition, let's take an example:

If the current url is http: // localhost: 8080/User

The user can perform different addition, deletion, modification, and query operations by requesting the same URL. For example

Http: // localhost: 8080/User? _ Method = get & id = 1001 so that the user information with id = 1001 in the Database user table can be obtained through the get request.

Http: // localhost: 8080/User? _ Method = post & id = 1001 & name = zhangsan so that a record can be inserted into the user table of the database.

Http: // localhost: 8080/User? _ Method = put & id = 1001 & name = lisi so that you can change the username id = 1001 in the user table to lisi

Http: // localhost: 8080/User? _ Method = delete & id = 1001 is used to delete the id = 1001 in the user table of the database.

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 <! -- The browser does not support put, delete, and other methods. The filter will/xxx? _ Method = delete to the standard http delete method --> 2 <filter> 3 <filter-name> hiddenHttpMethodFilter </filter-name> 4 <filter-class> org. springframework. web. filter. hiddenHttpMethodFilter </filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name> hiddenHttpMethodFilter </filter-name> 8 <url-pattern>/* </url-pattern> 9 </filter-mapping>

 

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 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 3 xmlns = "http://java.sun.com/xml/ns/javaee" 4 xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 version = "3.0"> 6 7 <! -- The browser does not support put, delete, and other methods. The filter will/xxx? _ Method = delete to the standard http delete method --> 8 <filter> 9 <filter-name> hiddenHttpMethodFilter </filter-name> 10 <filter-class> org. springframework. web. filter. hiddenHttpMethodFilter </filter-class> 11 </filter> 12 <filter-mapping> 13 <filter-name> hiddenHttpMethodFilter </filter-name> 14 <url-pattern>/* </url-pattern> 15 </filter-mapping> 16 17 <! -- This code can be put without the above --> 18 <! -- <Filter> 19 <filter-name> HttpMethodPutFilter </filter-name> 20 <filter-class> org. springframework. web. filter. httpPutFormContentFilter </filter-class> 21 </filter> 22 <filter-mapping> 23 <filter-name> HttpMethodPutFilter </filter-name> 24 <url-pattern>/* </url-pattern> 25 </filter-mapping> --> 26 27 28 <welcome-file-list> 29 <welcome-file>/index. jsp </welcome-file> 30 </welcome-file-list> 31 <! -- Spring MVC configuration --> 32 <servlet> 33 <servlet-name> spring </servlet-name> 34 <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> 35 36 <! -- Load-on-startup element indicates whether the container loads the servlet at startup (instantiate and call its init () method) --> 37 <load-on-startup> 1 </load-on-startup> 38 </servlet> 39 40 <servlet-mapping> 41 <servlet-name> spring </ servlet-name> 42 <url-pattern>/</url-pattern> 43 </servlet-mapping> 44 45 <! -- Spring configuration --> 46 <listener> 47 <listener-class> org. springframework. web. context. ContextLoaderListener </listener-class> 48 </listener> 49 50 <! -- Specifies the directory where the configuration file of Spring Bean is located. The default configuration is under the WEB-INF directory --> 51 <context-param> 52 <param-name> contextConfigLocation </param-name> 53 <param-value> classpath: applicationContext. xml </param-value> 54 </context-param> 55 </web-app>

 

Here we have configured the filter, and I have commented out it. If you use the following configuration file->

1 <! -- This code can be put without the above --> 2 <filter> 3 <filter-name> HttpMethodPutFilter </filter-name> 4 <filter-class> org. springframework. web. filter. httpPutFormContentFilter </filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name> HttpMethodPutFilter </filter-name> 8 <url-pattern>/* </url-pattern> 9 </filter-mapping>

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 <filter>2         <filter-name>hiddenHttpMethodFilter</filter-name>3         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>4     </filter>5     <filter-mapping>6         <filter-name>hiddenHttpMethodFilter</filter-name>7         <url-pattern>/*</url-pattern>8     </filter-mapping>

 

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.

 

This article is the original work of the seven small stations, reprint please indicate the source: http://www.cnblogs.com/qixiaoyizhan/ and the obvious position in the article page to give the original link, otherwise reserve the right to investigate legal liability.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.