REST(Representational State Transfer)描述了一个架构样式的网络系统,在目前主流的三种web服务交互方案中,REST相比于SOAP(Simple Object Access Proteocol,简单对象访问协议)以及XML-RPC更加简单明了,无论是对URL的处理还是对Payload的编码,REST都倾向于用更加简单轻量的方法设计和实现。质的注意的是REST并没有一个明确的标准,而更像是一个红设计的风格。它本身并没有什么实用性,器核心价值在于如何设计出符合REST风格的网络接口。
restful的优点:它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用。
restful的特性:
- 资源(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用来删除资源。
restful的示例:
- /account/1 HTTP GET: 得到id=1的account
- /account/1 HTTP DELETE: 删除id=1的account
- /account/1 HTTP PUT: 更新id=1的account
- /account HTTP POST: 新增account
PathVariable注解
作用:用于绑定url中的占位符。如:请求中/delete/{id},这个{id}就是url占位符。url支持占位符是Spring3.0之后加入的。是SpringMVC支持rest风格URL的一个重要标志。
属性:
- value:用于指定url中占位符名称。
- required:是否必须提供占位符。
使用示例:
<fieldset>
<h1>PathVariable注解</h1>
<a href="${pageContext.request.contextPath}/restful/usePathVariable/100">Path Variable注解</a>
</fieldset>
@Controller
@RequestMapping("/restful")
public class RestfulController {
@RequestMapping("/usePathVariable/{id}")
public String usePathVariable(@PathVariable(value = "id") String id){
System.out.println(id);
return "success";
}
}
HiddenHttpMethodFilter
作用:由于浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3.0添加了一个过滤器,可以将浏览器请求改为指定的请求方式,发送给控制器方法,使得支持GET、POST、PUT与DELETE请求。
使用方法:
- 第一步:在web.xml中配置该过滤器。
- 第二步:请求方式必须使用post请求。
- 第三步:按照要求提供_method请求参数,该参数的取值就是需要的请求方式。
源码分析:
示例:
在web.xml设置过滤器。
<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>
在客户端发送请求
<fieldset>
<h1>查询</h1>
<form action="${pageContext.request.contextPath}/restful/testRestfulGet/20" method="get">
<input type="submit" value="查询"/>
</form>
</fieldset>
<fieldset>
<h1>新增</h1>
<form action="${pageContext.request.contextPath}/restful/testRestfulPost" method="post">
username:<input type="text" name="username"/><br>
password:<input type="text" name="password"/><br>
age:<input type="text" name="age"/><br>
<input type="submit" value="新增"/>
</form>
</fieldset>
<fieldset>
<h1>更新</h1>
<form action="${pageContext.request.contextPath}/restful/testRestfulPut/20" method="post">
<input type="hidden" name="_method" value="PUT"/>
username:<input type="text" name="username"/><br>
password:<input type="text" name="password"/><br>
age:<input type="text" name="age"/><br>
<input type="submit" value="更新"/>
</form>
</fieldset>
<fieldset>
<h1>删除</h1>
<form action="${pageContext.request.contextPath}/restful/testRestfulDelete/10" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="删除"/>
</form>
</fieldset>
编写后端控制器
@Controller
@RequestMapping("/restful")
public class RestfulController {
@RequestMapping(value = "/testRestfulGet/{id}",method = RequestMethod.GET)
public String testRestfulGet(@PathVariable("id")String id){
System.out.println("restfut get "+id);
return "success";
}
@RequestMapping(value = "/testRestfulPost",method = RequestMethod.POST)
public String testRestfulPost(User user){
System.out.println("restful post "+user);
return "success";
}
@RequestMapping(value = "/testRestfulDelete/{id}",method = RequestMethod.DELETE)
public String testRestfulDelete(@PathVariable("id")Integer id){
System.out.println("restful delete "+id);
return "success";
}
@RequestMapping(value = "/testRestfulPut/{id}",method = RequestMethod.PUT)
public String testRestfulPut(@PathVariable("id")Integer id,User user){
System.out.println("restful put "+id+";"+user);
return "success";
}
}
由于以上只能在tomcat7上执行,tomcat8及以上运行时可以进入到相应的控制器,但是视图渲染返回的时候,由于不支持这两种方法,就会报错异常页面。