文章目录
- 一、Restful风格
 - 二、拦截器
 
- 1.简介
 - 2.使用
 
- 2.1定义拦截器
 - 2.2配置拦截条件
 - 2.3测试
 
- 3.拦截器工作原理
 
一、Restful风格
RESTful是一种软件设计规范,是客户端和服务端进行数据交互的一个规范。 早期使用JSP页面开发网页时,数据交互基本都是通过表单提交,然后通过内置对象传递。当HTML5兴起,移动互联网兴起,网站后端服务,不仅要考虑PC端的网页,也要考虑移动端数据的展示、小程序、HTML5页面等。如果需要多个终端(Android、iOS、小程序、Pad、HTML5页面)共用一个后端,一般来说主流方案就是使用JSON进行传递。RESTful则规范了请求的URL,注意RESTful只是一个规范,不是一个技术。
在RESTful中:
- 一个URL操作一个资源
 - 请求的URL中不能有动词
 - 使用HTTP的请求方式来描述请求行为,例如:
 
提交方式  | 地址  | 说明  | 
GET(查)  | http://localhost:8080/book/1  | 查询id为1的书  | 
POST(增)  | http://localhost:8080/book/1  | 添加一本书,书的id为1  | 
DELETE(删)  | http://localhost:8080/book/1  | 删除id为1的书  | 
PUT(改)  | http://localhost:8080/book/1  | 修改id为1的书  | 
在RESTful接口中,所有的方法都是返回JSON,没有返回页面的(ModelAndView),因此,所有的方法上都需要添加@ResponseBody注解。一个替代的简化方案,是使用 @RestController 代替@Controller。@RestController实际上是一个组合注解,是@Controller和@ResponseBody的组合:
案例代码
/**
 * RestFul
 * @author dpb【波波烤鸭】
 *
 */
public class UserController {
  
  UserService userService;
  /** * 查询所有、分页查询、条件查询 * 一般都是直接使用资源的复数形式来做路径 * * @return */
  ("/users")
  public List<User> getAllUser((defaultValue = "1") Integer page,
      (defaultValue = "4") Integer count) {
    return userService.getAllUser(page, count);
  }
  /**
   * * 按照id查询 例如 http://localhost:8080/user/1 表示查询id为1的用户 * * @param id
   * * @return
   */
  ("/user/{id}")
  public User getUserById( Integer id) {
    return userService.getUserById(id);
  }
  /** * 使用POST请求来完成添加功能 * * @param user * @return */
  ("/user")
  public RespBean addUser( User user) {
    int result = userService.addUser(user);
    if (result == 1) {
      return RespBean.ok("添加成功!");
    }
    return RespBean.error("添加失败!");
  }
  ("/user/{id}")
  public RespBean deleteUserById( Integer id) {
    int result = userService.deleteUserById(id);
    if (result == 1) {
      return RespBean.ok("删除成功!");
    }
    return RespBean.error("删除失败!");
  }
  ("/user")
  public RespBean updateUserById( User user) {
    int result = userService.updateUserById(user);
    if (result == 1) {
      return RespBean.ok("修改成功!");
    }
    return RespBean.error("修改失败!");
  }
}二、拦截器
1.简介
  SpringMVC中的拦截器对应了Web基础中的过滤器。
 拦截器和过滤器的区别:
序号  | 区别  | 
1  | 一般来说,如果使用了SpringMVC框架,然后有拦截器的需求, 建议使用拦截器而不是过滤器  | 
2  | 过滤器依赖于Servlet容器,而拦截器是SpringMVC自带的,不依赖容器  | 
3  | 拦截器的功能更为强大,因为拦截器是一种AOP风格的过滤器 (实际上这个功能过滤器也能实现,只是没有拦截器这么简单明了)  | 
2.使用
2.1定义拦截器
/**
 * 自定义拦截器
 * @author dpb【波波烤鸭】
 *
 */
public class FirstIntercepter implements HandlerInterceptor{
  /**
   * 进入Handler之前调用的方法
   * 处理:
   *    用于身份确认和授权
   *    比如确认当前请求是否登陆,如果登陆就方法,否则拦截跳回登陆界面
   * @return
   *    true 放过
   *    false 拦截
   */
  
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    System.out.println("preHandle 执行了...");
    return true;
  }
  /**
   * 进入Handler之后,返回ModelAndView对象之前执行
   * 可以修改调整的视图
   */
  
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
      ModelAndView modelAndView) throws Exception {
    // TODO Auto-generated method stub
    
    System.out.println("ModelAndView之前执行...");
    modelAndView.setViewName("/error.jsp");
    modelAndView.addObject("msg", "传递的信息...");
  }
  /**
   * 执行完成Handler之后执行此方法,
   * 应用场景:
   *    统一异常处理,统一日志处理,资源释放
   */
  
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
      throws Exception {
    System.out.println("执行完Handler到返回客户端之前执行...");
    
  }
}2.2配置拦截条件
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  <mvc:annotation-driven></mvc:annotation-driven>
  <context:component-scan base-package="com.dpb.*"></context:component-scan>
  
  <!-- 拦截器的配置 -->
  <mvc:interceptors>
    <mvc:interceptor>
      <!-- ** 表示当前目录及其子目录路径 -->
      <mvc:mapping path="/**"/>
      <bean class="com.dpb.interceptor.FirstIntercepter"></bean>
    </mvc:interceptor>
  </mvc:interceptors>
</beans>2.3测试
![在这里插入图片描述 SpringMVC教程6[Restful和拦截器]_SpringMVC_02](https://file.cfanz.cn/uploads/png/2022/07/01/2/1489dda12G.png)
![在这里插入图片描述 SpringMVC教程6[Restful和拦截器]_SpringMVC_03](https://file.cfanz.cn/uploads/png/2022/07/01/2/1RcOR6c9da.png)
![在这里插入图片描述 SpringMVC教程6[Restful和拦截器]_SpringMVC_04](https://file.cfanz.cn/uploads/png/2022/07/01/2/b51F2W1650.png)
3.拦截器工作原理
![在这里插入图片描述 SpringMVC教程6[Restful和拦截器]_spring_05](https://file.cfanz.cn/uploads/png/2022/07/01/2/Z26A9e94D1.png)
                
                










