Springboot第五章--接口架构风格restful

目录

一、是什么?

二、有什么用?

三、六个Rest里面使用的注解

四、具体怎么实现呢?

1、配置

2、前端

3、后端

四、完整代码(动力节点版)


一、是什么?

rest全称Representational State Transfer , 中文: 表现层状态转移

是一种接口风格和设计的理念,不是标准

二、有什么用?

我们常见以前写的url地址是这样格式的:http://localhost:8080/myboot?id=1

使用了rest的url地址是这样的http://localhost:8080/myboot/1

看起来更加简便了

三、六个Rest里面使用的注解

  @PathVariable :  从url中获取数据

 @GetMapping:    支持的get请求方式,  等同于 @RequestMapping( method=RequestMethod.GET)   用于查找数据

  @PostMapping:  支持post请求方式 ,等同于 @RequestMapping( method=RequestMethod.POST) 用于增加数据

  @PutMapping:  支持put请求方式,  等同于 @RequestMapping( method=RequestMethod.PUT) 用于修改数据

  @DeleteMapping: 支持delete请求方式,  等同于 @RequestMapping( method=RequestMethod.DELETE) 用于删除数据

  @RestController:  符合注解, 是@Controller 和@ResponseBody组合。

 

四、具体怎么实现呢?

1、配置

(针对springboot,ssm的话需要各位自己去搜它的配置)

在application.properties(yml)里面添加

#支持put,delete的启动
spring.mvc.hiddenmethod.filter.enabled=true

2、前端

   <h3>测试rest请求方式</h3>
    <form action="student/test" method="post">
        <input type="hidden" name="_method" value="put">
        <input type="submit" value="测试请求方式">
    </form>

这里跟以前不一样在于 多了一行   <input type="hidden" name="_method" value="put">,因为我们的method只有post和get,如果我们想使用put来和控制层的@PutMapping匹配,就要这样用

3、后端

package com.bjpowernode.controller;

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

@RestController
public class MyRestController {

    @PutMapping("/student/test")
    public String test(){
        return "执行了/student/test,使用put请求";
    }
}

四、完整代码(动力节点版)

1、整体结构

2、MyRestController

package com.bjpowernode.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 modifyStudent(@PathVariable Integer id,
                                @PathVariable Integer age){
        return "更新资源, 执行put请求方式: id="+id+"#age="+age;

    }

    /**
     * 删除资源
     */
    @DeleteMapping("/student/{id}")
    public String removeStudentById(@PathVariable Integer id){
        return "删除资源,执行delete, id="+id;
    }



    @PutMapping("/student/test")
    public String test(){
        return "执行了/student/test,使用put请求";
    }

    @DeleteMapping("/student/DeleteTest")
    public String DeleteTest(){
        return "执行了/student/DeleteTest,使用delete请求";
    }
}

 3、application.properties

server.port=9001
server.servlet.context-path=/myboot

#支持put,delete的启动
spring.mvc.hiddenmethod.filter.enabled=true

4、addStudent.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h3>添加学生</h3>
    <form action="student/zhangsan/20" method="post">
        <input type="submit" value="注册学生">
    </form>

</body>
</html>

 5、testrest.html

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

   <form action="student/DeleteTest" method="post">
       <input type="hidden" name="_method" value="delete">
       <input type="submit" value="删除方式">
   </form>
</body>
</html>

我只是看了这个视频后把源码和自己的思路总结一下,大家有兴趣的话可以看一下原视频

动力节点springboot视频教程-专为springboot初学者打造的教程_哔哩哔哩_bilibili你的三连就是录制视频的动力!一定不要忘记收藏、点赞、投币哦~本套视频基于SpringBoot2.4版本讲解。教程从细节入手,每个事例先讲解pom.xml中的重要依赖,其次application配置文件,最后是代码实现。让你知其所以,逐步让掌握SpringBoot框架的自动配置,starter起步依赖等特性。 为什么SpringBoot是创建Spring应用,必须了解spring-boot-starhttps://www.bilibili.com/video/BV1XQ4y1m7ex?p=66&spm_id_from=pageDriver

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值