Restful编码风格

╰+攻爆jí腚メ 2022-10-31 04:25 252阅读 0赞

restful编码风格

一、简介

1、RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。RESTFUL适用于移动互联网厂商作为业务接口的场景,实现第三方OTT调用移动网络资源的功能,动作类型为新增、变更、删除所调用资源。

优点:它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用,是目前最流行的一种互联网软件架构。

2、RESTFUL特点

①每一个URI代表1种资源;

②客户端使用GET、POST、PUT、DELETE4个表示操作方式的动词对服务端资源进行操作:GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源;

③通过操作资源的表现形式来操作资源;

④资源的表现形式是XML或者HTML;

⑤客户端与服务端之间的交互在请求之间是无状态的,从客户端到服务端的每个请求都必须包含理解请求所必需的信息。

3、REST这个词,是Roy Thomas Fielding在他2000年的博士论文中提出的。Fielding是一个非常重要的人,他是HTTP协议(1.0版和1.1版)的主要设计者、Apache服务器软件的作者之一、Apache基金会的第一任主席。所以,他的这篇论文一经发表,就引起了关注,并且立即对互联网开发产生了深远的影响。

二、restful编码风格

1、每一个URI代表一种资源

对用户的操作请求,http://localhost:8080/demo/user

对学生的操作请求,http://localhost:8080/demo/student

2、客户端通过四个HTTP动词,对服务器端资源进行操作,实现”表现层状态转化”。
get请求用于查询,post请求用于新增,put请求用于修改,delete请求用于删除































请求方式 请求url 说明
get http://localhost:8080/demo/user 查询用户信息
post http://localhost:8080/demo/user 添加新用户
put http://localhost:8080/demo/user 修改用户信息
delete http://localhost:8080/demo/user 删除用户信息

3、参数传递

方式一:get http://localhost:8080/demo/user?id=1 查询用户信息

方式二:get http://localhost:8080/demo/user/id/1 查询用户信息

方式三:get http://localhost:8080/demo/user/1 查询用户信息

4、响应json数据(包含状态码和状态信息)

可以响应html,xml,json,目前主流实用json

基本json格式:

  1. {
  2. code:200,
  3. mess:OK,
  4. data:具体数据,一般是字符串,对象,集合
  5. }

三、请求方式问题

1、http请求方式有8种:
在这里插入图片描述
patch修改部分属性,tomcat8及以上支持。

2、浏览器支持情况

(1)如果使用地址栏请求,请求方式是get。

(2)如果使用超链接,请求方式是get。

(3)如果使用表单提交,请求方式是get和post。

(4)如果使用ajax提交,请求方式可以任意指定。

3、如何解决表单提交请求方式问题

设置表单请求方式为post,然后在表单中加入hidden元素:name= “_method” value= “put或delete”

4、尽管ajax提交请求支持put和delete,但是传参服务器接收不到,解决方法同第3条。

5、服务器端处理put请求和delete请求

(1)编写过滤器,解决put和delete请求处理。

(2)使用springmvc框架,配置框架提供的过滤器。

(3)使用springboot框架,约定大于配置。

四、基于springmvc框架编写符合restful风格的接口

1、使用表单

表单页面:

  1. <html>
  2. <head>
  3. <title>restful测试</title>
  4. <meta charset="UTF-8">
  5. </head>
  6. <body>
  7. <h2>Hello World!</h2>
  8. <form action="http://localhost:8080/restDemo/postRest" method="post">
  9. <%--<form action="http://localhost:8080/restDemo/getRest" method="get">--%>
  10. <%--<form action="http://localhost:8080/restDemo/delRest" method="post">--%>
  11. <%--<form action="http://localhost:8080/restDemo/putRest" method="post">--%>
  12. <%-- <input type="hidden" name="_method" value="put">--%>
  13. <%-- <input type="hidden" name="_method" value="delete">--%>
  14. 姓名<input type="text" name="name"><br>
  15. 年龄<input type="text" name="age"><br>
  16. <input type="submit" value="提交">
  17. </form>
  18. <a href="http://localhost:8080/restDemo/getRest?name=zhangsan&age=30">测试get请求1</a>
  19. <a href="http://localhost:8080/restDemo/getRest2/zhangsan/30">测试get请求2</a>
  20. <a href="http://localhost:8080/restDemo/getRest3/zhangsan/30">测试get请求3</a>
  21. </body>
  22. </html>

controller 接口:

  1. package com.hdit.restDemo.controller;
  2. import com.hdit.restDemo.domain.User;
  3. import com.hdit.restDemo.util.ResponseData;
  4. import org.springframework.web.bind.annotation.*;
  5. /** * @author Administrator * @version v1.0 * @className RestController * @Date 2021/2/21 */
  6. @RestController
  7. public class RestControllerDemo {
  8. // @RequestMapping(value="putRest",method = RequestMethod.PUT)
  9. // 修改
  10. @PutMapping("/putRest")
  11. public String putRest(String name,Integer age){
  12. System.out.println(name);
  13. System.out.println(age);
  14. return "put success";
  15. }
  16. // 删除
  17. @DeleteMapping("/delRest")
  18. public String delRest(String name,Integer age){
  19. System.out.println(name);
  20. System.out.println(age);
  21. return "del success";
  22. }
  23. // 查询
  24. @GetMapping("/getRest")
  25. public String getRest(String name,Integer age){
  26. System.out.println(name);
  27. System.out.println(age);
  28. return "get success";
  29. }
  30. // 添加
  31. @PostMapping("/postRest")
  32. public String postRest(String name,Integer age){
  33. System.out.println(name);
  34. System.out.println(age);
  35. return "post success";
  36. }
  37. @GetMapping("/getRest2/{name}/{age}")
  38. public String getRest2(@PathVariable("name") String name ,@PathVariable("age") Integer age){
  39. System.out.println(name);
  40. System.out.println(age);
  41. return "get2 success";
  42. }
  43. @GetMapping("/getRest3/{name}/{age}")
  44. public ResponseData<User> getRest3(@PathVariable("name") String name ,@PathVariable("age") Integer age){
  45. User u = new User();
  46. u.setName(name);
  47. u.setAge(age);
  48. ResponseData<User> resp = new ResponseData<>();
  49. resp.setCode(200);
  50. resp.setMess("ok");
  51. resp.setData(u);
  52. return resp;
  53. }
  54. @GetMapping("/user/age/{age}")
  55. public String findUser1(@PathVariable("age") Integer age){
  56. System.out.println(age);
  57. return "get4 success";
  58. }
  59. @GetMapping("/user/name/{name}")
  60. public String findUser2(@PathVariable("name") String name){
  61. System.out.println(name);
  62. return "get4 success";
  63. }
  64. }

web.xml配置

  1. <!-- restful配置-->
  2. <filter>
  3. <filter-name>HiddenHttpMethodFilter</filter-name>
  4. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>HiddenHttpMethodFilter</filter-name>
  8. <url-pattern>/*</url-pattern> </filter-mapping>

2、使用ajax

  1. <html>
  2. <head>
  3. <title>restful测试</title>
  4. <meta charset="UTF-8">
  5. <script src="js/jquery.min.js"></script>
  6. </head>
  7. <body>
  8. <h2>Hello World!</h2>
  9. <input type="button" value="ajax" onclick="rest()">
  10. <script>
  11. function rest(){
  12. //使用get方式发送ajax请求
  13. // $.ajax({
  14. // type:"get",
  15. // url:"http://localhost:8080/restDemo/getRest2/zhangsan/30",
  16. // // data:"",
  17. // success:function(resp){
  18. // alert(resp);
  19. // }
  20. // });
  21. //使用post方式发送ajax请求
  22. // $.ajax({
  23. // type:"post",
  24. // url:"http://localhost:8080/restDemo/postRest",
  25. // data:{name:"zhangsan",age:33},
  26. // success:function(resp){
  27. // alert(resp);
  28. // }
  29. // });
  30. //直接使用put方式发送ajax请求,后端接口接收不到参数
  31. /* $.ajax({ type:"put", url:"http://localhost:8080/restDemo/putRest", data:{name:"zhangsan",age:33}, success:function(resp){ alert(resp); } });*/
  32. // 通过post方式,间接使用put发送ajax请求,可以接收到参数
  33. // $.ajax({
  34. // type:"post",
  35. // url:"http://localhost:8080/restDemo/putRest",
  36. // data:{name:"zhangsan",age:33,_method:"put"},
  37. // success:function(resp){
  38. // alert(resp);
  39. // }
  40. // });
  41. //直接使用delete方式发送ajax请求,后端接口接收不到参数
  42. $.ajax({
  43. type:"delete",
  44. url:"http://localhost:8080/restDemo/delRest",
  45. data:{ name:"zhangsan",age:33},
  46. success:function(resp){
  47. alert(resp);
  48. }
  49. });
  50. // 通过post方式,间接使用delete发送ajax请求,可以接收到参数
  51. // $.ajax({
  52. // type:"post",
  53. // url:"http://localhost:8080/restDemo/delRest",
  54. // data:{name:"zhangsan",age:33,_method:"delete"},
  55. // success:function(resp){
  56. // alert(resp);
  57. // }
  58. // });
  59. }
  60. </script>
  61. </body>
  62. </html>

发表评论

表情:
评论列表 (有 0 条评论,252人围观)

还没有评论,来说两句吧...

相关阅读

    相关 RESTful风格

    RESTful风格   Restful是一种软件设计规范,是客户端和服务端进行数据交互的一个规范。    早期使用JSP页面开发网页时,数据交互基本都是通过表单提...

    相关 REST风格

    目录 一、REST风格 1、设定http请求动作 2、设定请求参数(路径变量) -------------------- 一、REST风格 REST (Repr

    相关 RESTful风格

    写这篇,主要是之前呀,和团队一起接项目,我作为后台,既然把对数据的增删改查定义了4个不同uri接口。 所以我要用统一规范,对一个数据的增删改查只定义一个uri,不同的请求做相

    相关 restful风格

    概念          一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,

    相关 Restful风格

    什么是REST REST是英文representational state transfer(表象性状态转变)或者表述性状态转移;Rest是web服务的一种架构风格;使用