帮小伙伴推广:
字节跳动校招内推码: NH19EF9
投递链接: https://jobs.toutiao.com/s/L3bLQM3
接口:API(Application Programming Interface,应用程序接口)
是一些预先定义的接口 比如 函数,http接口,或指 软件系统不同部分衔接的约定。
开发无需访问源码,或者理解内部工作机制的细节。
架构风格:api组织方式(样子)
传统的:http://localhost:9002/my/addstudent?name=sunnygao&age=666
在地址上提供了访问的资源名称addstudent,在其后使用哪个了get方式传递参数。
RESTful架构风格
一种互联网软件架构设计的风格,但它不是标准,它只是提出了一组客户端和服务器交互时的架构理念和设计原则。
优点:
基于这种理念和原则设计的接口可以
更简洁
更有层次
任何的技术都可以实现这种理念,如果一个架构符合REST原则,就称它为RESTful架构。
REST中的要素:
用REST表示资源和对资源的操作。在互联网中,表示一个资源或者一个操作。
资源使用url表示的,在互联网,使用的图片,视频,文本,网页等都是资源。
资源是用名词表示。
对资源:
资源使用url表示,通过名词表示资源。
传统:http://localhost:9002/my/addstudent?name=sunnygao&age=666
在url中,使用名词表示资源,以及访问资源的信息,在url中,使用”/“分隔对资源的信息。
采用RESTful风格:http://localhost:9002/my/addstudent/sunnygao/666
使用http中的动作(请求方式),表示对资源的操作(CUBR)
GET: 查询资源 -select
http://localhost:9002/my/addstudent/sunnygao/666
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>
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>
DELETE:删除资源 -delete
<a href="http://localhost:8080/myboot/student/1">删除1的数据a>
需要的分页,排序等参数,依然放在url的后面,例如
http://localhost:9002/myboot/student?page=1&pageSize=20
使用url表示资源,使用http动作操作资源。
从url中获取数据
支持的get请求方式,等同于@RequestMapping(method=RequestMethod.GET)
支持的post请求方式,等同于@RequestMapping(method=RequestMethod.POST)
支持的put请求方式,等同于@RequestMapping(method=RequestMethod.PUT)
支持的delete请求方式,等同于@RequestMapping(method=RequestMethod.DELETE)
复合注解,是@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;
}
}
在springmvc中有一个过滤器,支持post请求转为put,delete
过滤器:org.springframework.web.filter.HttpPutFormContentFilter
作用:把请求中的post请求转为put,delete
application.properties(yml):开启适用HttpPutFormContentFilter过滤器
server.port=9001
server.servlet.context-path=/myboot
spring.mvc.hiddenmethod.filter.enabled=true
在请求页面,包含_method参数,他的值是put,delete,发起这个请求使用的post方式。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
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>
这种情况就会报错。
@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