REST风格
一、概念(非人话)
REST即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。
在三种主流的Web服务实现方案中,因为REST模式的Web服务与复杂的SOAP和XML-RPC对比来讲明显的更加简洁,越来越多的web服务开始采用REST风格设计和实现。例如,Amazon.com提供接近REST风格的Web服务进行图书查找;雅虎提供的Web服务也是REST风格的。
REST,翻译过来叫做表现层状态转化,是目前最流行的一个互联网软件架构,它架构清晰,符合标准,易于理解,扩展方便。
- 表现层(Representation):把资源具体呈现出来的形式,因此叫做表现层。
- 资源(Resource):网络上的一个具体信息,文本,图片,音频,视频都可以称之为资源,如果想要访问到互联网上的某一个资源,那么就必须要使用一个URL来唯一性的获取改资源,也可以这么说,URL是每一个资源的唯一标识符。
- 状态转化(State Transfer):当客户端发出一个请求的时候,就代表客户端跟服务端的一次交互过程,HTTP是一种无状态协议,即所有的状态都保存在服务器端,因此,客户端如果想要操作服务器,必须通过某些手段,让服务器的状态发生转化,而这种转化是建立在表现层的,这就是名字的由来(非人话)
二、人话
我们在获取资源的时候就是进行增删改查的操作,如果是原来的架构风格,需要发送四个请求,分别是:
- 查询:localhost:8080/query?id=1
- 增加:localhost:8080/insert
- 删除:localhost:8080/delete?id=1
- 更新:localhost:8080/update?id=1
按照此方式发送请求的时候比较麻烦,需要定义多种请求,而在HTTP协议中,有不同的发送请求的方式,分别是GET、POST、PUT、DELETE等,我们如果能让不同的请求方式表示不同的请求类型就可以简化我们的查询:
- GET:获取资源 /book/1
- POST:新建资源 /book
- PUT:更新资源 /book/1
- DELETE:删除资源 /book/1
三、使用
一切看起来都非常美好,但是大家需要注意了,我们在发送请求的时候只能发送post或者get,没有办法发送put和delete请求,那么应该如何处理呢?下面开始进入代码环节:
RestController.java
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;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Controller
public class RestController {
//POST请求映射成增加操作
@RequestMapping(value = "/user",method = RequestMethod.POST)
public String add(){
System.out.println("添加");
return "success";
}
//DELETE请求映射成删除操作
@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
System.out.println("删除:"+id);
return "success";
}
//PUT请求映射成更新修改操作
@RequestMapping(value = "/user/{id}",method = RequestMethod.PUT)
public String update(@PathVariable("id") Integer id){
System.out.println("更新:"+id);
return "success";
}
//GET请求映射成查询操作
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
public String query(@PathVariable("id") Integer id){
System.out.println("查询:"+id);
return "success";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
关联springmvc的配置文件:
此配置文件的属性可以不添加,但是需要在WEB-INF的目录下创建 前端控制器名称-servlet.xml文件
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--此过滤器完成请求方式的转换,将POST请求转换成PUT或者DELETE请求-->
<filter>
<filter-name>hiddenFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
rest.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/user" method="post">
<input type="submit" value="增加">
</form>
<form action="/user/1" method="post">
<input name="_method" value="delete" type="hidden">
<input type="submit" value="删除">
</form>
<form action="/user/1" method="post">
<input name="_method" value="put" type="hidden">
<input type="submit" value="修改">
</form>
<a href="/user/1">查询</a><br/>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
<title>Title</title>
</head>
<body>
666
</body>
</html>
四、总结
实际上定义了一种软件开发规范,就是把浏览器能够发送的不同请求映射成数据库的增删改查操作,方便我们进行软件开发。
- POST请求映射成增加操作
- DELETE请求映射成删除操作
- PUT请求映射成更新修改操作
- GET请求映射成查询操作
如果想要详细了解,可以去查看RESTFUL的使用,听说现在开发中都是使用RESTFUL,拿来即用即可,感兴趣的去了解,这里只做入门,不抻开来讲。