0
点赞
收藏
分享

微信扫一扫

初学SpringBoot--ch06-接口架构风格 RESTful

晗韩不普通 2022-02-19 阅读 56

ch06-接口架构风格 RESTful

接口: API(Application Programming Interface,应用程序接口)是一些预先定义的接口(如函数、HTTP接口),或指软件系统不同组成部分衔接的约定。 用来提供应用程序与开发人员基于某软件或硬件得以访问的一组例程,而又无需访问源码,或理解内部工作机制的细节。

接口(API): 可以指访问 servlet, controller 的 url, 调用其他程序的 函数

架构风格: api组织方式
就是一个传统的:

http://localhost:9002/mytrans/addStudent?name=lisi&age=26 

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

1.1 认识 RESTful

1.1.1 RESTful架构风格

REST :(英文:Representational State Transfer , 中文:表现层状态转移)。
REST:是一种接口的架构风格和设计的理念,不是标准。
优点: 更简洁,更有层次

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

1.2 RESTful 注解

Spring Boot 开发 RESTful 主要是几个注解实现

@PathVariable:获取 url 中的数据。
该注解是实现 RESTful 最主要的一个注解

@GetMapping:接收 get 方式的请求,等同于 @RequestMapping( method=RequestMethod.GET)。

@PostMapping :接收和处理 Post 方式的请求,等同于 @RequestMapping( method=RequestMethod.POST)。

@PutMapping:接收 put 方式的请求,可以用 PostMapping 代替,等同于 @RequestMapping( method=RequestMethod.PUT)。

@DeleteMapping:接收 delete 方式的请求,可以使用 GetMapping 代替,等同于 @RequestMapping( method=RequestMethod.DELETE)。

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

1.3 RESTful 风格的使用

1.3.1 加入Maven依赖

<dependencies>
		<!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		<!--测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

1.3.2 修改 application.properties 文件

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

1.3.3 编写Controller

package co.suyv.controller;

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

@RestController
public class MyRestController {

    /**
     * rest 中, url 要使用占位符,表示传递的数据。
     * 占位符叫做路径变量, 在 url 中的数据
     *
     * 格式: 在@RequestMapping 的 value 属性值中,使用 {自定义名称}
     * http://localhost:8080/myboot/student/1001/bj2009
     *
     *  @PathVariable: 路径变量注解,作用是获取 url 中的路径变量的值
     *       属性: value : 路径变量名称
     * 位置: 在逐个接收参数中,在形参定义的前面
     *
     * 注意:路径变量名和形参名一样, value 可以不写
     *
     */

    // 查询id=1001的学生
    @GetMapping("/student/{studentId}")
    public String queryStudent(@PathVariable(value = "studentId") Integer id){
        return "查询学生 studentId:" + id;
    }

    // 增加学生
    @PostMapping("/student/{name}/{age}")
    public String creatStudent(@PathVariable("name") String name,
                               @PathVariable("age") Integer age){
        return "创建student:姓名:"+ name + ",年龄:" +age;
    }

    // 修改学生
    @PutMapping("/student/{id}/{age}")
    public String modifyStudent(@PathVariable Integer id,
                                @PathVariable Integer age){
        return "修改student:id="+ "id" + ",age=" + age;
    }

    // 删除学生
    @DeleteMapping("/student/{id}")
    public String removeStudentById(@PathVariable Integer id){
        return "删除student:id=" + id;
    }
}

1.3.4 查询测试

在这里插入图片描述

1.3.5 添加测试

在这里插入图片描述

1.3.6 修改测试

在这里插入图片描述

1.3.7 删除测试

在这里插入图片描述

1.4 RESTful 优点

1.5 RESTful 总结

1.5.1 请求路径冲突

@GetMapping(value = "/student/{studentId}/{classname}")
@GetMapping(value = "/student/{studentId}/{schoolname}")

这样的路径访问会失败, 路径有冲突。

1.5.2 注意事项

举报

相关推荐

0 条评论