Restful编码风格

本文介绍了RESTful网络应用程序设计风格,强调其结构清晰、易于理解和扩展的特点。详细阐述了HTTP动词GET、POST、PUT和DELETE在资源操作中的应用,并提供了基于SpringMVC框架实现RESTful接口的例子,包括表单和Ajax请求的处理。此外,讨论了HTTP请求方式的扩展如PATCH以及在实际应用中的处理策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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说明
gethttp://localhost:8080/demo/user查询用户信息
posthttp://localhost:8080/demo/user添加新用户
puthttp://localhost:8080/demo/user修改用户信息
deletehttp://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格式:

{
    code:200,
    mess:OK,
    data:具体数据,一般是字符串,对象,集合
}

三、请求方式问题

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、使用表单

表单页面:

<html>
<head>
<title>restful测试</title>
<meta charset="UTF-8">
</head>
<body>
<h2>Hello World!</h2>
<form  action="http://localhost:8080/restDemo/postRest"  method="post">
<%--<form  action="http://localhost:8080/restDemo/getRest"  method="get">--%>
<%--<form  action="http://localhost:8080/restDemo/delRest"  method="post">--%>
<%--<form  action="http://localhost:8080/restDemo/putRest"  method="post">--%>
<%--    <input  type="hidden"  name="_method"   value="put">--%>
<%--    <input  type="hidden"  name="_method"   value="delete">--%>
    姓名<input  type="text"  name="name"><br>
    年龄<input   type="text"   name="age"><br>
    <input   type="submit"  value="提交">
</form>
<a  href="http://localhost:8080/restDemo/getRest?name=zhangsan&age=30">测试get请求1</a>
<a  href="http://localhost:8080/restDemo/getRest2/zhangsan/30">测试get请求2</a>
<a  href="http://localhost:8080/restDemo/getRest3/zhangsan/30">测试get请求3</a>
</body>
</html>

controller 接口:

package com.hdit.restDemo.controller;

import com.hdit.restDemo.domain.User;
import com.hdit.restDemo.util.ResponseData;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 * @version v1.0
 * @className RestController
 * @Date 2021/2/21
 */

@RestController
public class RestControllerDemo {

    // @RequestMapping(value="putRest",method = RequestMethod.PUT)
//    修改
    @PutMapping("/putRest")
    public   String  putRest(String  name,Integer   age){
        System.out.println(name);
        System.out.println(age);
        return   "put  success";
    }
//    删除
    @DeleteMapping("/delRest")
    public   String  delRest(String  name,Integer   age){
        System.out.println(name);
        System.out.println(age);
        return   "del  success";
    }
//    查询
    @GetMapping("/getRest")
    public   String  getRest(String  name,Integer   age){
        System.out.println(name);
        System.out.println(age);
        return   "get  success";
    }
//    添加
    @PostMapping("/postRest")
    public   String  postRest(String  name,Integer   age){
        System.out.println(name);
        System.out.println(age);
        return   "post  success";
    }
    @GetMapping("/getRest2/{name}/{age}")
    public String getRest2(@PathVariable("name") String name ,@PathVariable("age") Integer age){
        System.out.println(name);
        System.out.println(age);
        return  "get2 success";
    }
    @GetMapping("/getRest3/{name}/{age}")
    public ResponseData<User> getRest3(@PathVariable("name") String name ,@PathVariable("age") Integer age){
        User u = new User();
        u.setName(name);
        u.setAge(age);
        ResponseData<User> resp = new ResponseData<>();
        resp.setCode(200);
        resp.setMess("ok");
        resp.setData(u);
        return  resp;
    }

    @GetMapping("/user/age/{age}")
    public   String  findUser1(@PathVariable("age") Integer   age){

        System.out.println(age);
        return   "get4  success";
    }

    @GetMapping("/user/name/{name}")
    public   String  findUser2(@PathVariable("name") String  name){
        System.out.println(name);

        return   "get4  success";
    }

}

web.xml配置

  <!--  restful配置-->
  <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、使用ajax

<html>
<head>
<title>restful测试</title>
<meta charset="UTF-8">
<script src="js/jquery.min.js"></script>
</head>
<body>
<h2>Hello World!</h2>
<input   type="button"   value="ajax"  onclick="rest()">
<script>
    function   rest(){
        //使用get方式发送ajax请求
        // $.ajax({
        //     type:"get",
        //     url:"http://localhost:8080/restDemo/getRest2/zhangsan/30",
        //    // data:"",
        //     success:function(resp){
        //         alert(resp);
        //     }
        // });

        //使用post方式发送ajax请求
        // $.ajax({
        //     type:"post",
        //     url:"http://localhost:8080/restDemo/postRest",
        //     data:{name:"zhangsan",age:33},
        //     success:function(resp){
        //         alert(resp);
        //     }
        // });

       //直接使用put方式发送ajax请求,后端接口接收不到参数
       /* $.ajax({
            type:"put",
            url:"http://localhost:8080/restDemo/putRest",
            data:{name:"zhangsan",age:33},
            success:function(resp){
                alert(resp);
            }
        });*/
        
        // 通过post方式,间接使用put发送ajax请求,可以接收到参数
        // $.ajax({
        //     type:"post",
        //     url:"http://localhost:8080/restDemo/putRest",
        //     data:{name:"zhangsan",age:33,_method:"put"},
        //     success:function(resp){
        //         alert(resp);
        //     }
        // });

        //直接使用delete方式发送ajax请求,后端接口接收不到参数
        $.ajax({
            type:"delete",
            url:"http://localhost:8080/restDemo/delRest",
            data:{name:"zhangsan",age:33},
            success:function(resp){
                alert(resp);
            }
        });
        
        // 通过post方式,间接使用delete发送ajax请求,可以接收到参数
        // $.ajax({
        //     type:"post",
        //     url:"http://localhost:8080/restDemo/delRest",
        //     data:{name:"zhangsan",age:33,_method:"delete"},
        //     success:function(resp){
        //         alert(resp);
        //     }
        // });
    }	
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值