如何使用REST请求风格
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请求风格的更多相关文章
- 【源码分析】- 在SpringBoot中你会使用REST风格处理请求吗?
目录 前言 1.什么是 REST 风格 1.1 资源(Resources) 1.2 表现层(Representation) 1.3 状态转化(State Transfer) 1.4 综述 ...
- angular的post请求,SpringMVC后台接收不到参数值的解决方案
这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...
- 解决angular的post请求后SpringMVC后台接收不到参数值问题的方法
这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...
- @RequestMapping映射请求,@PathVariable,@RequestParam,@RequestHeader的使用
1.@RequestMapping Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请求,在控制器的类定义及方法定义处都可标注. @RequestMa ...
- 如何用Spring框架的<form:form>标签实现REST风格的增删改查操作
1.首先创建两个bean类,Employee(职工)和Department(部门),一个部门可以有多个职工 Employee类(属性:职工ID:id:姓名:lastName:邮箱:email:性别:g ...
- RESTFul是一种风格
只要符合RESTFul风格的,都可以叫做使用了RESTFul架构,一般的网站里传数据,都是用的?a=1&b=2...如果是RESTFul风格的话,就会是/a/1/b/2..类似于这样的方式来传 ...
- wsdl说明书
WSDL文档的结构实例解析 <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns ...
- wsdl 结构
WSDL文档可以分为两部分.分别是抽象部分和具体描述 部分. 抽象部分 抽象部分以独立于平台和语言的方式定义SOAP消息,它们并不包含任何随 机器或语言而变的元素.<types>.< ...
- Web Service学习笔记(webservice、soap、wsdl、jws详细分析)
Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...
随机推荐
- EasyNVR depends on ffmpeg,yasm/nasm not found or too old. Use --disable-yasm for a crippledbuild
安装ffmpeg过程中,执行./configure时,报yasm/nasm not found or too old. Use --disable-yasm for a crippledbuild错误 ...
- 批量索引以提高索引速度 -d --data-binary
index create update 第1.2行分别为:信息行.数据行,在索引中增加或更换文档delete 移除文档,只包含信息行 Bulk API | Elasticsearch Referenc ...
- 原文来自 url get
w http://www.tuicool.com/articles/BvYbEvR http://36kr.com/p/5069371.html?utm_source=tuicool&utm_ ...
- DIV背景图片
.bigY{ position:absolute; width:95px; height:93px; visibility:visible; right: 277 ...
- 【题解】Digit Tree
[题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. ...
- 设置mysql外网访问
任意主机以用户root和密码mypwd连接到mysql服务器mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'myp ...
- 使用selenium进行密码破解(绕过账号密码JS加密)
经常碰到网站,账号密码通过js加密后进行提交.通过burp拦截抓到的账号密码是加密后的,所以无法通过burp instruder进行破解.只能模拟浏览器填写表单并点击登录按钮进行破解.于是想到了自动化 ...
- MySQL错误日志提示innodb_table_stats和innodb_index_stats不存在故障处理
查看MySQL error日志,发现有如下报错 7efbc586f700 InnoDB: Error: Table "mysql"."innodb_table_stats ...
- 隐马尔可夫模型(hidden Markov model,HMM)
定义: 隐马尔科夫模型是关于时序的概率模型,描述由一个隐藏的马尔可夫链随机生成不可观测的状态随机序列,再由各个状态生成一个观测而产生观测随机序列的过程. 隐马尔科夫模型由初始概率分布.状态转移概率分布 ...
- 内网IP&外网IP/NAT
内网的计算机以NAT(网络地址转换)协议,通过一个公共的网关访问Internet.内网的计算机可向Internet上的其他 计算机发送连接请求,但Internet上其他的计算机无法向内网的计算机发送连 ...