@每天都要敲代码

@每天都要敲代码

目录 

一:接口架构风格—RESTful

1. 认识RESTful

2. RESTful 的注解


一:接口架构风格—RESTful

1. 认识RESTful

(1)接口

(2)架构风格

(3)REST架构风格

资源使用URL表示,通过名词表示资源:

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

GET: 查询资源 ---> sql select

POST: 创建资源 ---> sql insert

<form action="http://localhost:8080/myboot/student" method="post">
	姓名:<input type="text" name="name" />
    年龄:<input type="text" name="age" />
</form>

PUT:更新资源 ---> sql update

<form action="http://localhost:8080/myboot/student/1" method="post">
	姓名:<input type="text" name="name" />
    年龄:<input type="text" name="age" />
    <input type="hidden" name="_method" value="PUT" />
</form>

DELETE: 删除资源 ---> sql delete

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

总结:使用url表示资源 ,使用http动作操作资源!

(4)分页

(5)优点

2. RESTful 的注解

案例:@PathVariable注解和@GetMapping注解的联合使用(查询资源)

package com.zl.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {

    @GetMapping("/student/{stuId}")
    public String queryStudent(@PathVariable(value = "stuId") Integer stuId){
        return "Studnet的id是:"+stuId;

    }
}

执行结果:

【SpringBoot】| 接口架构风格—RESTful-LMLPHP

案例:@PathVariable注解和@PostMapping注解的联合使用(创建资源)

addStudent.html表单页面(属于静态资源放到static目录下),在post请求中传递数据

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

接收数据

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

执行结果:

【SpringBoot】| 接口架构风格—RESTful-LMLPHP

案例:对于Post、Put、Delete都需要编写一个表单页面,比较麻烦,并且对于put和delete请求浏览器是不支持的;可以借助一个Postman工具

    // 更新资源
    @PutMapping("/student/{id}/{age}")
    public String modifyStudent(@PathVariable Integer id,
                                @PathVariable Integer age){
        return "更新资源Student:"+id+"="+age;
    }


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

执行结果:省去写表单页面了

【SpringBoot】| 接口架构风格—RESTful-LMLPHP

案例:使用HiddenHttpMethodFilter过滤器,将post请求转为put ,delete

第一步:在application.properties(yml) : 开启使用HiddenHttpMethodFilter过滤器

package org.springframework.boot.autoconfigure.web.servlet;

public class WebMvcAutoConfiguration {
	@Bean
	@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
	@ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled")
	public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
		return new OrderedHiddenHttpMethodFilter();
	}
}

设置为true

#启用过滤器
spring.mvc.hiddenmethod.filter.enabled=true

第二步:在请求页面中,发出post请求;type类型使用隐藏域hidden,name参数是 _method, value对应着我们真正的请求方式put、delete

form表单页面,实际上真正发出的是put请求!

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

扩展:当前也可以把_method参数设置为自定义参数

【SpringBoot】| 接口架构风格—RESTful-LMLPHP

所以就可以自己创建一个HiddenHttpMethodFilter,调用setMethodParam方法自己定义

package com.zl.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.HiddenHttpMethodFilter;
 
@Configuration(proxyBeanMethods = false)
public class MyConfig {
    // 自定义Filter,纳入容器管理
    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
        HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();
        // 设置method参数为_m
        methodFilter.setMethodParam("_m");
        return methodFilter;
    }
 
}

问题:请求路径冲突

例如:以下两个请求,使用get请求,资源名也相同,携带的数据类型也相同;我们直接进行访问:http://localhost:8081/student/1;此时就会有路径冲突,导致访问失败!

解决:设计路径,必须唯一, 路径uri和请求方式必须唯一!

@GetMapping("/student/{id}") 
@GetMapping("/student/{age}")
08-14 17:53