Rest:系统希望以非常简洁的URL地址来发请求;
怎样表示对一个资源的增删改查用请求方式来区分
/getBook?id=1 :查询图书
/deleteBook?id=1:删除1号图书
/updateBook?id=1:更新1号图书
/addBook :添加图书
Rest推荐;
url地址这么起名; /资源名/资源标识符
/book/1 :GET-----查询1号图书
/book/1 :PUT------更新1号图书
/book/1 :DELETE-----删除1号图书
/book :POST-----添加图书
系统的URL地址就这么来设计即可;
简洁的URL提交请求,以请求方式区分对资源操作;
问题:从页面上只能发起两种请求,GET、POST;
其他的请求方式没法使用;
使用Rest来构建一个增删改查系统;
页面地址:
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 { /** * 发起图书的增删改查请求;使用Rest风格的URL地址; * 请求url 请求方式 表示含义 * /book/1 GET: 查询1号图书 * /book/1 DELETE:删除1号图书 * /book/1 PUT: 更新1号图书 * /book POST: 添加1号图书 * 从页面发起PUT、DELETE形式的请求 */ /** * 处理查询图书请求 * @param id * @return */ @RequestMapping(value="/book/{bid}",method=RequestMethod.GET) public String getBook(@PathVariable("bid")Integer id) { System.out.println("查询到了"+id+"号图书"); return "success"; } /** * 图书删除 * @param id * @return */ @RequestMapping(value="/book/{bid}",method=RequestMethod.DELETE) public String deleteBook(@PathVariable("bid")Integer id) { System.out.println("删除了"+id+"号图书"); return "success"; } /** * 图书更新 * @return */ @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"; } }
部分源码分析:
1 @Override 2 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 3 throws ServletException, IOException { 4 //获取表单上_method带来的值(delete\put) 5 String paramValue = request.getParameter(this.methodParam); 6 7 //判断如过表单是一个post而且_method有值 8 if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) { 9 //转为PUT、DELETE 10 String method = paramValue.toUpperCase(Locale.ENGLISH); 11 //重写了request.getMethod(); 12 HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method); 13 14 //wrapper.getMethod()===PUT; 15 filterChain.doFilter(wrapper, response); 16 } 17 else { 18 //直接放行 19 filterChain.doFilter(request, response); 20 } 21 }
从页面发起PUT、DELETE形式的请求?Spring提供了对Rest风格的支持
1)、SpringMVC中有一个Filter;他可以把普通的请求转化为规定形式的请求;配置这个filter;
1 <filter> 2 <filter-name>HiddenHttpMethodFilter</filter-name> 3 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>HiddenHttpMethodFilter</filter-name> 7 <url-pattern>/*</url-pattern> 8 </filter-mapping>
2)、如何发其他形式请求?
按照以下要求;1、创建一个post类型的表单 2、表单项中携带一个_method的参数,3、这个_method的值就是DELETE、PUT
完整的web.xml:
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 6 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 7 id="WebApp_ID" version="3.0"> 8 <display-name>3.SpringMVC_rest</display-name> 9 <welcome-file-list> 10 <welcome-file>index.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- The front controller of this Spring Web application, 14 responsible for handling all application requests --> 15 <servlet> 16 <servlet-name>springDispatcherServlet</servlet-name> 17 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 18 19 <init-param> 20 <!-- contextConfigLocation:指定SpringMVC配置文件位置 --> 21 <param-name>contextConfigLocation</param-name> 22 <param-value>classpath:springmvc.xml</param-value> 23 </init-param> 24 <load-on-startup>1</load-on-startup> 25 </servlet> 26 27 <!-- Map all requests to the DispatcherServlet for handling --> 28 <servlet-mapping> 29 <servlet-name>springDispatcherServlet</servlet-name> 30 <url-pattern>/</url-pattern> 31 </servlet-mapping> 32 33 <filter> 34 <filter-name>HiddenHttpMethodFilter</filter-name> 35 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 36 </filter> 37 <filter-mapping> 38 <filter-name>HiddenHttpMethodFilter</filter-name> 39 <url-pattern>/*</url-pattern> 40 </filter-mapping> 41 42 </web-app>
springmvc.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 7 8 <!-- 扫描所有组件 --> 9 <context:component-scan base-package="com.atguigu"></context:component-scan> 10 11 <!-- 配置一个视图解析器 ;能帮我们拼接页面地址--> 12 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 13 <property name="prefix" value="/WEB-INF/pages/"></property> 14 <property name="suffix" value=".jsp"></property> 15 </bean> 16 </beans>
index.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- 发起图书的增删改查请求;使用Rest风格的URL地址; 11 请求url 请求方式 表示含义 12 /book/1 GET: 查询1号图书 13 /book/1 DELETE: 删除1号图书 14 /book/1 PUT: 更新1号图书 15 /book POST: 添加1号图书 16 17 从页面发起PUT、DELETE形式的请求?Spring提供了对Rest风格的支持 18 1)、SpringMVC中有一个Filter;他可以把普通的请求转化为规定形式的请求;配置这个filter; 19 <filter> 20 <filter-name>HiddenHttpMethodFilter</filter-name> 21 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 22 </filter> 23 <filter-mapping> 24 <filter-name>HiddenHttpMethodFilter</filter-name> 25 <url-pattern>/*</url-pattern> 26 </filter-mapping> 27 2)、如何发其他形式请求? 28 按照以下要求;1、创建一个post类型的表单 2、表单项中携带一个_method的参数,3、这个_method的值就是DELETE、PUT 29 30 --> 31 <a href="book/1">查询图书</a><br/> 32 <form action="book" method="post"> 33 <input type="submit" value="添加1号图书"/> 34 </form><br/> 35 36 37 <!-- 发送DELETE请求 --> 38 <form action="book/1" method="post"> 39 <input name="_method" value="delete"/> 40 <input type="submit" value="删除1号图书"/> 41 </form><br/> 42 43 <!-- 发送PUT请求 --> 44 <form action="book/1" method="post"> 45 <input name="_method" value="put"/> 46 <input type="submit" value="更新1号图书"/> 47 </form><br/> 48 </body> 49 </html>
注意:高版本Tomcat;Rest支持有点问题:
![](https://i-blog.csdnimg.cn/blog_migrate/ce796b5e37494115416a421792ad54c8.png)
解决方案:
success.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" isErrorPage="true" %> 3 <%--isErrorPage="true":解决高版本tomcatdelete、put请求的405问题--%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <h1>你成功了,6666</h1> 12 </body> 13 </html>