SpringBoot框架详解(五)接口风格 -RESTful

帮小伙伴推广:

字节跳动校招内推码: NH19EF9
投递链接: https://jobs.toutiao.com/s/L3bLQM3

SpringBoot框架详解(五)接口风格 -RESTful

  1. REST:
    1. 英文:Representation State Transfer
    2. 中文:表现层状态转移
      1. 表现层就是视图层,显示资源的,通过视图页面,jsp等等显示操作资源的结果。
      2. 状态:资源变化
      3. 转移:资源可以变化的。资源能创建,new状态,资源创建后可以查询资源,能看到资源的内容,资源的内容可以被修改,修改后资源和之前的不一样。

第五章 接口风格 -RESTful

1. RESTful

接口:API(Application Programming Interface,应用程序接口)

是一些预先定义的接口 比如 函数,http接口,或指 软件系统不同部分衔接的约定。

开发无需访问源码,或者理解内部工作机制的细节。

  1. 可以指访问servlet,controller的url
  2. 调用其他程序的函数。

架构风格:api组织方式(样子)

  1. 传统的:http://localhost:9002/my/addstudent?name=sunnygao&age=666

    在地址上提供了访问的资源名称addstudent,在其后使用哪个了get方式传递参数。

  2. RESTful架构风格

    ​ 一种互联网软件架构设计的风格,但它不是标准,它只是提出了一组客户端服务器交互时的架构理念和设计原则。

    优点:

    ​ 基于这种理念和原则设计的接口可以

    1. 更简洁

    2. 更有层次

      任何的技术都可以实现这种理念,如果一个架构符合REST原则,就称它为RESTful架构。

  3. REST中的要素:

    用REST表示资源和对资源的操作。在互联网中,表示一个资源或者一个操作。

    资源使用url表示的,在互联网,使用的图片,视频,文本,网页等都是资源。

    资源是用名词表示。

  4. 对资源:

    1. 查询资源:看,通过url找到资源。
    2. 创建资源:添加资源。
    3. 更新资源:更新资源,编辑。
    4. 删除资源:去除。

资源使用url表示,通过名词表示资源。

传统:http://localhost:9002/my/addstudent?name=sunnygao&age=666

在url中,使用名词表示资源,以及访问资源的信息,在url中,使用”/“分隔对资源的信息。

采用RESTful风格:http://localhost:9002/my/addstudent/sunnygao/666

使用http中的动作(请求方式),表示对资源的操作(CUBR)

  1. GET: 查询资源 -select

    http://localhost:9002/my/addstudent/sunnygao/666

  2. POST:创建资源 -insert

    http://localhost:9002/my/addstudent/sunnygao

    在post请求中传递数据。

    <form action="http://localhost:9002/myboot/student" method="post">
      姓名:<input type="text" name="name"/>
      年龄:<input type="text" name="age"/>
    </form>
    
  3. PUT:更新资源 -updata

    <form action="http://localhost:9002/myboot/student/1" method="post">
      姓名:<input  type="text" name="name"/>
      年龄:<input type="text" name="age"/>
      <input type="hidden" name="_method" value="PUT"/>
    </form>
    
  4. DELETE:删除资源 -delete

    <a href="http://localhost:8080/myboot/student/1">删除1的数据</a>
    

需要的分页,排序等参数,依然放在url的后面,例如

http://localhost:9002/myboot/student?page=1&pageSize=20

使用一句话说明REST

​ 使用url表示资源,使用http动作操作资源。

注解

1. @PathVariable

从url中获取数据

2. @GetMapping

支持的get请求方式,等同于@RequestMapping(method=RequestMethod.GET)

3. @PostMapping

支持的post请求方式,等同于@RequestMapping(method=RequestMethod.POST)

4. @PutMapping

支持的put请求方式,等同于@RequestMapping(method=RequestMethod.PUT)

5. @DeleteMapping

支持的delete请求方式,等同于@RequestMapping(method=RequestMethod.DELETE)

6. @RestController

复合注解,是@Controller和@ResponseBody组合。在类上面使用@RestController,表示当前类的所有方法都加入了@Controller和@ResponseBody。

package com.firewolf.controller;

import org.springframework.web.bind.annotation.*;

@RestController
public class MyRestController {
//    学习注解的使用
//    查询id=1001的学生

    /**
     * @PathVariable (路径变量):获取url中的数据
     *      属性:value :路径变量名
     *      位置:放在控制器方法的形参面前
     *
     * http://localhost:8080/myboot/student/1002
     *
     * {stuId}:定义路径变量,stuId自定义名称。
     * **/
    @GetMapping("/student/{stuId}")
    public String queryStudent(@PathVariable("stuId") Integer studentId){
        return "查询学生studentId="+studentId;
    }

    /**
     * 创建资源 Post 请求方式
     * http://localhost:8080/myboot/student/zhangsan/20
     * **/

    @PostMapping("/student/{name}/{age}")
    public String createStudent(@PathVariable("name") String name,
                                @PathVariable("age") Integer age){
        return "创建资源 student:name="+name+"#age="+age;
    }

    /**
     * 更新资源
     *
     * 当路径变量名称和形参名一样,@PathVariable 中的value 可以省略
     * **/
    @PutMapping("/student/{id}/{age}")
    public String modifyStudnet(@PathVariable("id") Integer id,
                                @PathVariable("age") Integer age){
        return "更新资源 执行put请求student:id="+id+"#age="+age;
    }

    /**
     * 删除资源
     * **/
    @DeleteMapping("/student/{id}")
    public String removeStudnetById(@PathVariable("id") Integer id) {
        return "删除资源 student:id=" + id;
    }

}

2. 在页面中或者ajax中,支持pub,delete请求。

在springmvc中有一个过滤器,支持post请求转为put,delete

过滤器:org.springframework.web.filter.HttpPutFormContentFilter

作用:把请求中的post请求转为put,delete

1.实现步骤

  1. application.properties(yml):开启适用HttpPutFormContentFilter过滤器

    server.port=9001
    server.servlet.context-path=/myboot
    spring.mvc.hiddenmethod.filter.enabled=true
    
  2. 在请求页面,包含_method参数,他的值是put,delete,发起这个请求使用的post方式。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <h3>添加学生</h3>
        <form action="student/test" method="post">
            <input type="hidden" name="_method" value="put">
            <input type="submit" value="put测试请求方式">
        </form>
        <br/>
        <form action="student/testdelete" method="post">
            <input type="hidden" name="_method" value="delete">
            <input type="submit" value="delete测试请求方式">
        </form>
    </body>
    </html>
    

2.避免出现歧义

这种情况就会报错。

    @GetMapping("/student/{stuId}")
    public String queryStudent(@PathVariable("stuId") Integer studentId){
        return "查询学生studentId="+studentId;
    }

    @GetMapping("/student/{age}")
    public String queryStudentByAge(@PathVariable("age") Integer age){
        return "查询学生age="+age;
    }

之后学习完redis之后继续更新springBoot+redis

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值