RESTful style API Interface Development SPRINGMVC Chapter

Source: Internet
Author: User

RESTful API is a software architecture style, design style rather than standard, but provides a set of design principles and constraints. It is mainly used for client and server interaction classes of software. The design based on this style of software can be more concise, more layered, more easy to implement caching mechanisms.

In restful style, the URL requested by the user is used in the same URL: get,post,delete,put ... and other ways to distinguish the processing of requests, so that in the development of pre-background separation, the front-end developers will not be confused with the requested resource address and a large number of check method name trouble, form a unified interface.

In restful style, the existing rules are as follows:

    • GET (SELECT): From the server query, you can distinguish the way the query is queried by the parameters requested by the server.
    • POST (Create): Creates a new resource on the server and invokes the insert operation.
    • PUT (UPDATE): Updates the resource on the server, calling the update operation.
    • PATCH (UPDATE): Updates the resource on the server (the client provides changed properties). (currently jdk7 not implemented, TOMCAT7 also not).
    • Delete: Deletes the resource from the server and invokes the DELETE statement.

After understanding this style definition, let's give an example:

If the current URL is Http://localhost:8080/User

Then the user can only request such a URL to implement different additions and deletions, such as

Http://localhost:8080/User?_method=get&id=1001 This allows you to get the user information for id=1001 in the database user table through a GET request

Http://localhost:8080/User?_method=post&id=1001&name=zhangsan This allows you to insert a record into the database User table

Http://localhost:8080/User?_method=put&id=1001&name=lisi This allows you to change the user name of id=1001 within the users table to Lisi

Http://localhost:8080/User?_method=delete&id=1001 This is used to remove id=1001 information from the Database User table

This definition of the specification we can call the RESTful API interface, we can use the same URL to achieve a variety of operations.

Next we explain how to implement the RESTful API interface in SPRING-MVC, and solve the problems that arise in it! (The Java Web does not support problems with put and delete requests)

First we build the Project interface for spring MVC and write the controller in restful style, here I write a user controller class and a user "Action"

The Controller and Action URL address here is the access address written in restful style/user/user method

Our front desk uses jquery Ajax for requests,

Someone's going to ask? Why the delete and put is also a POST request, here is to say that the Java inside the put and delete do not support

Java is originally a put and delete requests to filter out (do not know why to do so), and in the servlet there are doget,dopost,dodelete,doput corresponding methods, but can not be used (awkward not embarrassing), The same spring MVC also has corresponding method=requestmethod.put and delete, but Ajax inside the type write PUT, delete is accessible to the corresponding method, but the parameters can not pass the past, All arguments passed in the past are null (depressed not depressed)! C # will not be so, C # API programming need to open a put and delete on it, do not need to be so complicated in Java, here we solve this problem

First, add a filter to the SPRINGMVC project's Web. xml

1 <!--browser does not support Put,delete method, the filter converts/xxx?_method=delete to the standard HTTP Delete methods-->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 novices don't know where this code is added, so I'll paste my web. xml here (I've been doing this for a while ...). )

 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://jav A.sun.com/xml/ns/javaee/web-app_3_0.xsd "5 version=" 3.0 "> 6 7 <!--browser does not support Put,delete and other method, the filter will be the/X Xx?_method=delete conversion to standard HTTP Delete method--8 <filter> 9 <filter-name>hiddenhttpmethodfilter</f Ilter-name>10 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter>12 <filter-mapping>13 <filter-name>hiddenhttpmethodfilter</filter-name&gt ; <url-pattern>/*</url-pattern>15 </filter-mapping>16 <!--This code can be put if you don't use it. -->18 <!--<filter>19 <filter-name>httpmethodputfilter</filter-name>20 <filt Er-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 <welcome-file-list>29 <welcome-file>/index.jsp</welcome-file& Gt;30 </welcome-file-list>31 <!--Spring MVC configuration-->32 <servlet>33 <servlet-name&gt ; spring</servlet-name>34 <servlet-class>org.springframework.web.servlet.dispatcherservlet</ Servlet-class>35 <!--the Load-on-startup element tag container will load this servlet at startup (instantiate and invoke its init () method)-->37 <loa d-on-startup>1</load-on-startup>38 </servlet>39 <servlet-mapping>41 <servlet-na     me>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 <!--Specifies the directory where the spring bean's configuration file resides.         The default configuration is in 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-ap P>

Here we have the filter configured, I have a comment off, if using the following configuration file,

1  <!--This code can implement put-->2 <filter>3 if not used above         <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>

This configuration item can support a put request if it is written here, but the delete request is still not available, so I can only choose 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>

The method of this paragraph is to standardize the HTTP request with the Org.springframework.web.filter.HiddenHttpMethodFilter's built-in filter class. This gives us the means to declare the request ourselves.

After the configuration is done, we need to pass a parameter _method in Ajax: "PUT" and _method: "DELETE", but the request is still post

With this configuration, we can already access the method of the delete modification, as well as _method: ' Put ' we can access the put modified method so that the Controller class we have defined above can be implemented.

This article is seven small station main original works, reprint please indicate source: http://www.cnblogs.com/qixiaoyizhan/and in the article page obvious position gives the original link, otherwise reserves the right to pursue legal responsibility.

RESTful Style API interface Development SPRINGMVC article

Related Article

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.