REST即(资源)表现层状态转化(英文:(Resource)Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,能够下降开发的复杂性,提升系统的可伸缩性。目前在三种主流的Web实现方案中,由于REST模式的Web服务与复杂的SOAP和XML-RPC对比来说明显的更加简洁,愈来愈多的web服务开始采用REST风格设计和实现。例如,Amazon.com提供接近REST风格的Web服务进行图书查找;雅虎提供的Web服务也是REST风格的。html
资源(Resource):任何事物,只要有被引用到的必要,它就是一个资源。资源能够是实体,也能够是一个抽象概念。能够用URI(统一资源定位符)指向它,每种资源对应一个特定的URI。要获取这个资源,访问它的URI便可,所以URI及为每个资源的独一无二的识别符。java
表现层(Representation):把资源具体呈现出来的形式,叫作它的表现层。好比:文本能够用txt格式表现,也能够用HTML格式、XML格式、JSON格式表现,甚至能够采用二进制格式web
状态转化(State Transfer):每发出一个请求,就表明了客户端和服务器端的一次交互过程。HTTP协议,是一个无状态协议,即全部的状态都保存在服务器端。所以,若是客户端想要操做服务器,必须经过某种手段让服务器端发生“状态转化”。这种转化是创建在表现层之上的,因此就是“表现层状态转化”。spring
遵照REST体系结构约束的Web服务API称为RESTful API。基于HTTP的RESTful API定义有如下几个方面:浏览器
RESTful能够经过一套统一的接口为Web、iOS、Android提供服务。服务器
以CRUD为例:网络
咱们以前的方法请求多是这样的:架构
news?id=1 获得id=1的newsmvc
newsDelete?id=1 删除id=1的newsapp
newsUpdate?id=1 修改id=1的news
newsAdd 添加news
而REST风格的API增删改查都是一个地址,具体操做靠HTTP头部信息判断:
*/news/1 HTTP GET:获得id=1的news
*/news/1 HTTP DELETE:删除id=1的news
*/news/1 HTTP PUT:更新id=1的news
*/news HTTP POST:新增news
浏览器form表单只支持GET和POST请求,Spring3.0以后添加了一个过滤器(HiddenHttpMethodFiler),能够将这些请求转化为标准的HTTP方法,使得支持GET、POST、PUT与DELETE请求
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter:能够把POST请求转为DELETE或POST请求 --> <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> <!-- 配置前置控制器 --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- DispatcherServlet的一个初始化参数:配置SpringMVC 配置文件的位置和名称 --> <!-- 实际上也能够不经过contextConfigLocation来配置SpringMVC的配置文件,而使用默认的 默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml springDispatcherServlet-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
SpringMVCTest.java
package com.f145a.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("/springmvc") @Controller public class SpringMVCTest { private static final String SUCCESS="success"; /** * REST风格的URL * 以CRUD为例: * 新增:/order POST * 修改:/order/1 PUT update?id=1 * 获取:/order/1 GET get?id=1 * 删除:/oeder/1 DELETE delete?id=1 * * 如何发送PUT请求和DELETE请求呢? * 1.须要配置HiddenHTTPMethodFilter * 2.须要发送POST请求 * 3.须要在发送POST请求时携带一个name="_method"的隐藏域,值为DELETE或PUT * * 在SpringMVC的目标方法中如何获得id呢? * 使用@PathVariable注解 */ @RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT) public String testRestPut(@PathVariable Integer id){ System.out.println("testRest PUT:"+id); return SUCCESS; } @RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE) public String testRestDelete(@PathVariable Integer id){ System.out.println("testRest Delete:"+id); return SUCCESS; } @RequestMapping(value="/testRest",method=RequestMethod.POST) public String testRest(){ System.out.println("testRest POST"); return SUCCESS; } @RequestMapping(value="/testRest/{id}",method=RequestMethod.GET) public String testRest(@PathVariable Integer id){ System.out.println("testRest GET:"+id); return SUCCESS; } }
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="springmvc/testRest/1" method="post"> <input type="hidden" name="_method" value="PUT"> <input type="submit" value="TestRest PUT"> </form> <form action="springmvc/testRest/1" method="post"> <input type="hidden" name="_method" value="DELETE"> <input type="submit" value="TestRest DELETE"> </form> <form action="springmvc/testRest" method="post"> <input type="submit" value="TestRest POST"> </form> <a href="springmvc/testRest/1">TestRest GET</a> </body> </html>
欢迎访问个人我的博客http://www.chengzequn.top