What is rest? Let's start with paragraph introduction.
REST: Representational state Transfer. (Resource) Presentation Layer State transformation. is currently one of the most popular Internet software architectures. Its structure is clear, conforms to the standard, easy to understand, expands conveniently, therefore is getting more and more website adoption.
Resources : An entity on the network, or a specific information on the network. It can be a piece of text, a picture, a song, a service, in short, a specific existence. You can point to it with a URI (Uniform Resource Locator), and each resource corresponds to a specific URI. To get this resource, you can access its URI, so the URI is a unique identifier for each resource .
Presentation layer (representation) : The form in which resources are presented is called its presentation layer (representation). For example, text can be expressed in the TXT format , or it can be expressed in HTML, XML, JSON format, or even in binary format.
State Transfer : Each time a request is made, it represents an interactive process between the client and the server . HTTP protocol, is a stateless protocol, that is, all the state is saved on the server side. Therefore, if the client wants to operate the server, there must be some means for the server side to occur "
State Transfer. And this transformation is based on the expression layer, so is the " Expression Layer state transformation." Specifically, there are four verbs in the HTTP protocol that represent the mode of operation: GET, POST, PUT, DELETE. They correspond to four basic operations: get is used to get resources, POST is used to create new resources, PUT is used to update resources, and delete is used to delete resources. (This article originates from: http://my.oschina.net/happyBKs/blog/416994)
Example:
–/ORDER/1 HTTP get: Get order with id = 1
–/ORDER/1 HTTP Delete: Delete the order with id = 1
–/ORDER/1 HTTP PUT: Update id = 1 for order
–/order HTTP POST: New Order
But to implement four methods with spring requires a filter:
hiddenhttpmethodfilter: The browser form form only supports get and POST requests, while the delete, PUT, etc method is not supported, Spring3.0 adds a filter that can convert these requests to a standard HTTP method, which enables GET, POST, PUT, and delete requests.
The URL with placeholders is a new feature of Spring3.0, which is a milestone in the development process of SPRINGMVC to REST goals .
By @PathVariable You can bind a placeholder parameter in a URL to an entry in the handler processing method: the {XXX} placeholder in the URL can be bound to the entry of an action method by @PathVariable ("xxx").
Well, don't say much, we're actually practicing, adding hiddenhttpmethodfilter filters to the original web. XML in the WebApp directory
<! doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en " " HTTP://JAVA.SUN.COM/DTD/WEB-APP_2_3.DTD " ><web-app> < display-name>archetype created web application</display-name> <!-- Configuration Org.springframework.web.filter.HiddenHttpMethodFilter, yes. Post requests are converted to put or delete requests--><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> <!-- Configure dispatcherservlet --> <!-- The following code is automatically generated for STS, if it is general Eclipse needs to install Springide plug-in --> <!--&NBSP;THE&NBSP;FRONT&NBSp;controller of this spring web application, responsible for handling all application requests --><servlet><servlet-name>springdispatcherservlet </servlet-name><servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class ><!-- Configure an initialization parameter for Dispatcherservlet: Configure springmvc Configuration location and name --><init-param>< param-name>contextconfiglocation</param-name><param-value>classpath:springmvc.xml</ param-value><!-- classpath springmvc.xml --></init-param><!-- Load-on-startup refers to the servlet being created when the current Web application is loaded, rather than being created the first time it is requested --><load-on-startup>1</ load-on-startup></servlet><!-- Map all requests to the dispatcherservlet for handling/delegates can answer all requests, handled by Springdispatcherservlet --><servlet-mapping ><servlet-name>springdispatcherservlet</servlet-namE><url-pattern>/</url-pattern></servlet-mapping></web-app>
Springmvc.xml compared to the examples in the previous article, there is no change, here for the convenience of readers are also given, about SPRINGMVC project structure and configuration please refer to my previous article.
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/ context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/ Schema/mvc/spring-mvc-4.1.xsd "><!-- Configure automatically scanned packages --><context:component-scan Base-package= "Com.happyBKs.springmvc.handlers" ></context:component-scan><!-- Configure the View resolver: How to handler the return value of the method parse to actual physical view--><bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" prefix " Value= "/views/"></property><property name=" suffix " value=". JSP "></property></bean ></beans>
Next write the controller class and its methods to receive and process the request: Resttesthandler class
Package com.happybks.springmvc.handlers;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestMethod, @RequestMapping ("/rest") @Controllerpublic class RestTestHandler {/* * Rest style of url Originally using the style of request parameters * Crud For example * add: /order post * modify:/order/1 put update?id=1 * get:/order/1 get get?id=1 * Delete:/order/1 deletedelete?id=1 * * How do I send put requests and delete requests? * 1. Need to configure HIDDENHTTPMETHODFILTER&NBSP;*&NBSP;2. A POST request * 3 needs to be sent. Need to carry a name= when sending a POST request "_method "In the hidden field, the value is delete or put * * how to get the ID in the target method of Springmvc? * using @pathvariable annotations */@RequestMapping (value= "/methodstest/{id}", Method=requestmethod.get) Public&nbSp String restget (@PathVariable int id)//When @pathvariable is not marked {id},{system.out.println ("get " +id ); System.out.println ("Querry operations ...");return "Querry";} @RequestMapping (value= "/methodstest", Method=requestmethod.post) Public string restpost () { System.out.println ("post "); System.out.println ("Post operations ...");return "POST";} @RequestMapping (value= "/methodstest/{id}", Method=requestmethod.put) Public string restput (@ Pathvariable int id) {System.out.println ("put " +id); System.out.println ("Put operations ...");return "put";} @RequestMapping (value= "/methodstest/{id}", Method=requestmethod.delete) Public string restdelete (@ Pathvariable int id) {System.out.println ("delete " +id); System.out.println ("Delete operations ...");return "delete";}}
<%@ page language= "java" contenttype= "Text/html; charset=iso-8859-1" pageencoding= "Iso-8859-1"%><! doctype html public "-//w3c//dtd html 4.01 transitional//en" "HTTP// Www.w3.org/TR/html4/loose.dtd ">Have you noticed? Hiddenhttpmethodfilter helps us to simulate put requests and delete requests. It translates post requests with hidden domain _method to put requests and delete requests to the server.
Process:
Click on the four Link and form buttons:
Console input is:
Get 1querry operations...post post operations...put 1put operations...delete 1delete operations ...
Summarize:
The Rest-style URL originally takes the style of the request parameter
* Take crud as an example
* Added:/order POST
* Modified:/ORDER/1 Put update?id=1
* Get:/ORDER/1 get get?id=1
* Delete:/ORDER/1 deletedelete?id=1
* How do I send put requests and delete requests?
1. Need to configure Hiddenhttpmethodfilter
2. Need to send a POST request
3. Need to carry a hidden field name= "_method" when sending a POST request with a value of delete or put
* How to get the ID in the target method of Springmvc?
Using @pathvariable annotations
——————————————————————————————
Note: I'm using a TOMCAT7 server. TOMCAT8 does not seem to support put and delete requests, even though I have added the ReadOnly parameter and set to False in the Conf folder of Tomcat, it still does not solve the problem.
<servlet> <servlet-name> Default</servlet-name> <servlet-class> org.apache.catalina.servlets.defaultservlet</servlet-class> <init-param> <param-name >debug</param-name> < param-value>0</param-value> </init-param>< init-param> <param-name>readonly</param-name> <param-value>false</ Param-value> </init-param> <init-param > <param-name>listings</ Param-name> <param-value>false</param-value> </init-param> <load-on-startup>1 </load-on-startup> </servlet>
The results of clicking the POST request and the delete request are:
SPRINGMVC Note Series (7)--hiddenhttpmethodfilter filter