REST
简介
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 用来删除资源。
HiddenHttpMethodFilter
浏览器 form 表单只支持 GET 与 POST 请求,而DELETE、PUT 等 method 并不支持,Spring3.0 添加了一个过滤器,可以将这些请求转换为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。
在web.xml文件中配置过滤器
<!-- 处理Rest请求的过滤器 -->
<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>
发送请求
<a href="${pageContext.request.contextPath}/testPathVariable/zhangshan/18">测试@PathVariable</a><br>
处理请求
/**
* @PathVariable注解作用:将Restful风格占位符中的参数,映射给方法的形参
* /testPathVariable/zhangshan/18
* /testPathVariable/{username}/{age}
*/
@RequestMapping("/testPathVariable/{username}/{age}")
public String testPathVariable(@PathVariable("username") String username,
@PathVariable("age") Integer age) {
System.out.println(username + ":::" + age);
return "success";
}
增加请求
<form action="${pageContext.request.contextPath}/order/100/baidu" method="post">
<input type="submit" value="新增订单">
</form>
<form action="${pageContext.request.contextPath}/order/100" method="post">
<input type="hidden" name="_method" value="delete">
<input type="submit" value="删除订单">
</form>
<form action="${pageContext.request.contextPath}/order/100" method="post">
<input type="hidden" name="_method" value="put">
<input type="submit" value="修改订单">
</form>
<form action="${pageContext.request.contextPath}/order/100" method="get">
<input type="submit" value="查询订单">
</form>
package helloWorld;
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;
/**
* 使用Restful风格实现订单的增删改查
* 增 - post
* 删 - delete
* 改 - put
* 查 - get
*/
@Controller
public class OrderController {
/**
* 新增订单
* http://localhost:8080/springmvc01/order/100/商品订单
*/
@RequestMapping(value = "/order/{id}/{name}",method = RequestMethod.POST)
public String addOrder(@PathVariable("id")Integer id,@PathVariable("name")String name) {
System.out.println("新增订单==>订单id" + id +",订单名称" + name);
return "success";
}
/**
* 删除订单
* http://localhost:8080/springmvc01/order/100
*/
@RequestMapping(value = "/order/{id}",method = RequestMethod.DELETE)
public String deleteOrder(@PathVariable("id") Integer id) {
System.out.println("删除订单==>id:" + id);
return "success";
}
/**
* 修改订单
* http://localhost:8080/springmvc01/order/100
*/
@RequestMapping(value = "/order/{id}",method = RequestMethod.PUT)
public String updateOrder(@PathVariable("id") Integer id) {
System.out.println("修改订单==>id:" + id);
return "success";
}
/**
* 查询订单
* http://localhost:8080/springmvc01/order/100
*/
@RequestMapping(value = "/order/{id}",method = RequestMethod.GET)
public String selectOrder(@PathVariable("id") Integer id) {
System.out.println("查询订单==>id:" + id);
return "success";
}
}