RESTful风格

作者: 凡哥爱丽姐 | 来源:发表于2020-12-03 15:02 被阅读0次
1、通过http的请求状态来确定请求的目的(自己的理解)
2、RESTful架构风格规定,数据的元操作,即CRUD(create, read, update和delete,即数据的增删查改)操作,分别对应于HTTP方法:GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源,这样就统一了数据操作的接口,仅通过HTTP方法,就可以完成对数据的所有增删查改工作。
- GET用来获取资源
- POST用来创建新资源
- PUT用来更新资源
- DELETE用来删除资源

比如:

  • /order/1 HTTP GET :得到id = 1 的 order
  • /order/1 HTTP DELETE: 删除 id=1 的order
  • /order/1 HTTP PUT : 更新id = 1的 order
  • /order/1 HTTP POST : 新增 order
3、浏览器form表单只支持GET和POST,不支持DELETE和PUT请求,Spring添加了一个HiddenHttpMethodFilter过滤器,可以将这些请求转换为标准的http方法,支持GET,POST,DELETE,PUT请求!
4、web.xml添加HiddenHttpMethodFilter配置
<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>
5、一个RESTful架构风格的例子
5.1、配置pom.xml
<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <!-- Spring的核心工具包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <!--在基础IOC功能上提供扩展服务,还提供许多企业级服务的支持,有邮件服务、任务调度、远程访问、缓存以及多种视图层框架的支持-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <!-- Spring IOC的基础实现,包含访问配置文件、创建和管理bean等 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <!-- Spring context的扩展支持,用于MVC方面 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <!-- Spring表达式语言 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <!--springAop开发必须加入的包-->
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.13</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
  </dependencies>
5.2、配置web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加载spring配置文件,原来是通过 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--处理post乱码-->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>
      org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>

    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--Resultful风格  添加了一个过滤器,可以将这些请求转换为标准的http方法,支持GET,POST,DELETE,PUT请求!-->
  <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>
</web-app>
5.3、配置spring.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="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.xsd
                           ">

    <!--扫描controller-->
    <context:component-scan base-package="com"></context:component-scan>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--jsp所在位置-->
        <property name="prefix" value="/"></property>
        <!--jsp文件后缀名-->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
5.4、index.jsp和success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
get:<a href="/test/order">get</a><br>
post:<form method="post" action="/test/order">
       <input type="submit" value="post">
     </form><br>
put:<form method="post" action="/test/order">
      <input type="hidden" name="_method" value="put">
      <input type="submit" value="put">
    </form><br>
delete:<form method="post" action="/test/order">
        <input type="hidden" name="_method" value="delete">
        <input type="submit" value="delete">
     </form>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: Mr Wei
  Date: 2020/10/28
  Time: 13:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>测试成功!</h1>
</body>
</html>
5.5、TestController测试类
package com.fan.controller;

import com.sun.net.httpserver.Authenticator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

    @GetMapping("/test/order")
    @RequestMapping(value = "/test/order",method = RequestMethod.GET)
    public String test11(){
        System.out.println("get--/test/order");
        return "success";
    }

    @PostMapping("/test/order")
    @RequestMapping(value = "/test/order",method = RequestMethod.POST)
    public String test22(){
        System.out.println("post--/test/order");
        return "success";
    }

    @RequestMapping(value = "/test/order",method = RequestMethod.PUT)
    public String test33(){
        System.out.println("put--/test/order");
        return "redirect:/success.jsp";
    }

    @RequestMapping(value = "/test/order",method = RequestMethod.DELETE)
    public String test44(){
        System.out.println("delete--/test/order");
        return "redirect:/success.jsp";
    }
}

相关文章

网友评论

    本文标题:RESTful风格

    本文链接:https://www.haomeiwen.com/subject/lnpqvktx.html