REST 是什么
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 风格
示例:Rest推荐ur地址起名方式:/资源名/资源标识符
/order/1 HTTP GET : 得 到 id = 1 的 order
/order/1 HTTP DELETE:删除 id = 1 的 order
/order HTTP PUT:更新 order
/order HTTP POST:新增 order
3)HiddenHttpMethodFilter
浏览器 form 表单只支持 GET 与 POST 请求,而 DELETE、PUT 等 method 并不支持,Spring3.0 添加了一个过滤器,可以将这些请求转换为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。
4)参考资料
理解 REST 架构风格
REST
Rest风格的增删改查
问题:从页面上只能发起两种请求:GET、POST
从页面发起PUT、DELETE形式的请求该如何做?spring提供了Rest风格的支持
1)springmvc中有个filter,他可以吧普通的请求转化为规定形式的请求,配置这个filter
<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)如何发其他形式的请求?
①创建一个post表单,
②表单携带一个_method的参数
③这个_method的值就是DELETE、PUT
<a href="book/1">查询图书</a><br/>
<form action="book" method="post">
<input type="submit" value="添加1号图书"/>
</form>
<form action="book/1" method="post">
<input name="_method" value="delete">
<input type="submit" value="删除1号图书"/>
</form>
<form action="book/1" method="post">
<input name="_method" value="put">
<input type="submit" value="更新1号图书"/>
HiddenHttpMethodFilter 过滤器源码分析
1)为什么请求隐含参数名称必须叫做”_method”
2)hiddenHttpMethodFilter 的处理过程
Rest风格的增删改查代码
1)配置HiddenHttpMethodFilter 过滤器
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>
2)代码
BookController .java
package com.atguigu.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 BookController {
//查询图书
@RequestMapping(value="/book/{bid}",method=RequestMethod.GET)
public String getBook(@PathVariable("bid")Integer id) {
System.out.println("查询到了"+id+"号图书");
return "success";
}
//删除图书
@RequestMapping(value="/book/{bid}",method=RequestMethod.DELETE)
public String deleteBook(@PathVariable("bid") Integer id) {
System.out.println("删除了"+id+"号图书");
return "success";
}
//更新图书
@RequestMapping(value="/book/{bid}",method=RequestMethod.PUT)
public String updateBook(@PathVariable("bid") Integer id) {
System.out.println("更新了"+id+"号图书");
return "success";
}
//添加图书
@RequestMapping(value="/book",method=RequestMethod.POST)
public String addBook() {
System.out.println("添加了新的图书");
return "success";
}
}
3)请求链接
index.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="book/1">查询图书</a><br/>
<form action="book" method="post">
<input type="submit" value="添加1号图书"/>
</form>
<form action="book/1" method="post">
<input type="hidden" name="_method" value="delete">
<input type="submit" value="删除1号图书"/>
</form>
<form action="book/1" method="post">
<input type="hidden" name="_method" value="put">
<input type="submit" value="更新1号图书"/>
</form>
</body>
</html>
success.jsp
<!--
注意:高版本的Tomcat的Rest-JSP不接受
DELETE\PUT格式请求的问题,解决方法,在
jsp页面的头部添加isErrorPage="true"
-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>你成功了</h1>
</body>
</html>
运行期间出现的问题
1、文件位置放错,导致页面出现404错误,且显示“源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。”
解决办法:
springmvc的配置文件我在web.xml上是没有说明位置(contextconfigLocation),因此默认配置文件位于/WEB-INF/(serclet-name)-servlet.xml;
web.xml文件在WEB-INF目录下
index.xml在WebContent目录下
2、因为我的tomcat是9.0的,不支持Rest风格下的Jsp页面接受DELETE、PUT形式的请求问题
解决方案:在success.jsp页面的头部添加isErrorPage=“true”
3、因为之前文件放错了位置,导致我多次启动和关闭多个tomcat,这时候我就出现了tomcat端口号被占用和tomcat启动时间过长导致出错
解决方案: