REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。
它结构清晰、符合标准、易于理解、扩展方便, 所以正得到越来越多网站的采用。
HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。
它们分别对应四种基本操作:

  • 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 HTTP POST:新增 order

浏览器 form 表单只支持 GET 与 POST 请求,而DELETE、PUT 等 method 并不支持,Spring3.0 添加了一个过滤器HiddenHttpMethodFilter,可以将这些请求转换为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。
POST请求如何转化为put请求和delele请求?
1、在web.xml文件中配置:

 <!-- 配置springMVC -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置SpringMVC加载的配置文件(配置处理器映射器、适配器等等)-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析
第二种:/,所有访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析
使用此种方式可以实现 RESTful风格的URL
第三种:/*,这样配置不对,使用这种配置,最终要转发到一个JSP页面时, 仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错。 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- HiddenHttpMethodFilter过滤器可以将POST请求转化为put请求和delete请求! -->
<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、在表单域中需要携带一个name值为_method,value值为put或者delete的参数,get和post方法正常使用,如下所示:

 <!-- REST-----GET -->
<a href="${pageContext.request.contextPath }/order/1">提交get请求</a>
<!-- REST-----POST -->
<form action="${pageContext.request.contextPath }/order/1" method="post">
<input type="submit" value="提交POST请求">
</form>
<!-- REST-----PUT -->
<form action="${pageContext.request.contextPath }/order/1" method="post">
<input type="hidden" name="_method" value="put">
<input type="submit" value="提交put请求">
</form>
<!-- REST-----DELETE -->
<form action="${pageContext.request.contextPath }/order/1" method="post">
<input type="hidden" name="_method" value="delete">
<input type="submit" value="提交delete请求">
</form>

3、在后台接收参数并作处理,

@RequestMapping中的参数名称必须和@PathVariable中value的值相同。
 public static String SUCCESS="success";//WEB-INF下的success.jsp
//REST请求方式-----GET获取
@RequestMapping(value="/order/{id}",method=RequestMethod.GET)
public String helloGet(@PathVariable(value="id") Integer id){
System.out.println("GET-------"+id);
return SUCCESS;
}
//REST请求方式-----POST添加
@RequestMapping(value="/order/{id}",method=RequestMethod.POST)
public String helloPost(@PathVariable(value="id") Integer id){
System.out.println("POST-------"+id);
return SUCCESS;
}
//REST请求方式-----PUT修改
@RequestMapping(value="/order/{id}",method=RequestMethod.PUT)
public String helloPut(@PathVariable(value="id") Integer id){
System.out.println("PUT-------"+id);
return SUCCESS;
}
//REST请求方式-----DELETE删除
@RequestMapping(value="/order/{id}",method=RequestMethod.DELETE)
public String helloDelete(@PathVariable(value="id") Integer id){
System.out.println("DELETE-------"+id);
return SUCCESS;
}

如何使用REST请求风格的更多相关文章

  1. 【源码分析】- 在SpringBoot中你会使用REST风格处理请求吗?

    ​ 目录 前言 1.什么是 REST 风格 1.1  资源(Resources) 1.2  表现层(Representation) 1.3  状态转化(State Transfer) 1.4  综述 ...

  2. angular的post请求,SpringMVC后台接收不到参数值的解决方案

    这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...

  3. 解决angular的post请求后SpringMVC后台接收不到参数值问题的方法

    这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...

  4. @RequestMapping映射请求,@PathVariable,@RequestParam,@RequestHeader的使用

    1.@RequestMapping Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请求,在控制器的类定义及方法定义处都可标注. @RequestMa ...

  5. 如何用Spring框架的<form:form>标签实现REST风格的增删改查操作

    1.首先创建两个bean类,Employee(职工)和Department(部门),一个部门可以有多个职工 Employee类(属性:职工ID:id:姓名:lastName:邮箱:email:性别:g ...

  6. RESTFul是一种风格

    只要符合RESTFul风格的,都可以叫做使用了RESTFul架构,一般的网站里传数据,都是用的?a=1&b=2...如果是RESTFul风格的话,就会是/a/1/b/2..类似于这样的方式来传 ...

  7. wsdl说明书

    WSDL文档的结构实例解析 <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns ...

  8. wsdl 结构

    WSDL文档可以分为两部分.分别是抽象部分和具体描述 部分. 抽象部分 抽象部分以独立于平台和语言的方式定义SOAP消息,它们并不包含任何随 机器或语言而变的元素.<types>.< ...

  9. Web Service学习笔记(webservice、soap、wsdl、jws详细分析)

    Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...

随机推荐

  1. Python 使用MySQL

    在导入MySQLdb之前,需要安装MySQLdb模块.使用pip安装,命令如下: pip install MySQL-python 安装成功后,导入MySQLdb模块 import MySQLdb 连 ...

  2. 1624 取余最长路(set)

    1624 取余最长路 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 佳佳有一个n*m的带权矩阵,她想从(1,1)出发走到(n,m)且只能往右往下移动,她能得到的娱 ...

  3. javascript中字符串截取的两种方法

    var testStr = "hello kay!"; 1.substr testStr.substr(1)   ->ello kay! testStr.substr(1,4 ...

  4. 浅谈Spring框架注解的用法分析

    原文出处: locality 1.@Component是Spring定义的一个通用注解,可以注解任何bean. 2.@Scope定义bean的作用域,其默认作用域是”singleton”,除此之外还有 ...

  5. django实现密码加密的注册(数据对象插入)

    在 django实现密码非加密的注册(数据对象插入)的基础上,我们对视图和注册页面进行了简单修改 视图 from django.shortcuts import render,redirect,ren ...

  6. 避免每次都用sudo使用docker

    默认安装完 docker 后,每次执行 docker 都需要运行 sudo 命令,非常浪费时间影响效率.如果不跟 sudo,直接执行 docker images 命令会有如下问题: FATA[0000 ...

  7. 设计线程安全的类 VS 发布线程安全的对象

    一.设计线程安全的类 步骤: 找出构成对象状态的所有变量 找出约束状态变量的不变性条件 建立对象状态的并发访问策略 1.在现有的线程安全类中添加功能 (1)重用能减低工作量和提高正确性 (2)如果底层 ...

  8. git原理:.git隐藏文件夹介绍

    config 定义项目特有的配置选项description 仅供git web程序使用info/ 包含一个全局排除文件(exclude文件),用于配置不在.gitignore中的忽略模式hooks/ ...

  9. ./bin/console server:run Surprise! There are no commands defined in the "server" namespace.

    Let's start the built-in web server:   ./bin/console server:run Surprise! There are no commands defi ...

  10. 20170421 F110 常见问题

    F110常見問題以及處理方式 1. Vendor中沒有與F110中相同的Payment method 解決辦法: 在Vendor主檔中維護Payment method 2. 結報被Block 解決辦法 ...