1) REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用
① 资源(Resources):网络上的一个实体,或者说是网络上的一个具体信息。
它可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的存在。
可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的 URI 。
获取这个资源,访问它的URI就可以,因此 URI 即为每一个资源的独一无二的识别符。
② 表现层(Representation):把资源具体呈现出来的形式,叫做它的表现层(Representation)。比如,文本可以用 txt 格式表现,也可以用 HTML 格式、XML 格式、JSON 格式表现,甚至可以采用二进制格式。
③ 状态转化(State Transfer):每发出一个请求,就代表了客户端和服务器的一次交互过程。HTTP协议,是一个无状态协议,即所有的状态都保存在服务器端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“状态转化”(State Transfer)
而这种转化是建立在表现层之上的,所以就是 “表现层状态转化”。
④ 具体说,就是 HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。
它们分别对应四种基本操作:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。
2)URL风格
示例:
/order/1 HTTP GET :得到 id = 1 的 order gerOrder?id=1
/order/1 HTTP DELETE:删除 id = 1的 order deleteOrder?id=1
/order HTTP PUT:更新order
/order HTTP POST:新增 order
扩展: 开放平台 支付功能: 你应该发送什么URL ,需要传递什么参数, 需要有哪些验证 ,响应哪些数据.
3)HiddenHttpMethodFilter
浏览器 form 表单只支持 GET 与 POST 请求,而DELETE、PUT 等 method 并不
支持,Spring3.0 添加了一个过滤器,可以将这些请求转换为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。
如何发送PUT请求或DELETE请求?
- ①.配置HiddenHttpMethodFilter
- ②.需要发送POST请求
- ③.需要在发送POST请求时携带一个 name="_method"的隐含域,值为PUT或DELETE
1、 配置HiddenHttpMethodFilter过滤器
<?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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>SpringMVC-02</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
2、jsp网页代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="testREST/404">测试GET</a>
<form action="testREST" method="post">
<input type="submit" value="测试POST"/>
</form>
<form action="testREST" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="测试PUT"/>
</form>
<form action="testREST/1001" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="测试DELETE"/>
</form>
</body>
</html>
3、测试类
在SpringMVC的目标方法中如何得到id值呢?
- 使用@PathVariable注解
在web.xml中配置如下:
package com.blh.controller;
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;
@Controller
public class RestController {
@RequestMapping(value="/testREST/{id}",method = RequestMethod.GET)
public String getUserById(@PathVariable("id")Integer id) {
System.out.println("GET:id:"+id);
return "success";
}
@RequestMapping(value="/testREST",method = RequestMethod.POST)
public String getuser() {
System.out.println("POST");
return "success";
}
@RequestMapping(value="/testREST",method = RequestMethod.PUT)
public String insertuser() {
System.out.println("PUT");
return "success";
}
@RequestMapping(value="/testREST/{id}",method = RequestMethod.DELETE)
public String deleteuser(@PathVariable("id")Integer id) {
System.out.println("DELETE:id"+id);
return "success";
}
}