FastAPI学习笔记(一)-5.当2个接口的调用方法和路由相同时,按照前后顺序,只执行第一个接口

/tutorial/chapter03.py

 1 '''
 2 @author:invoker
 3 @project:fastapi202108
 4 @file: chapter03.py
 5 @contact:invoker2021@126.com
 6 @descript:
 7 @Date:2021/8/5 21:22
 8 @version: Python 3.7.8
 9 '''
10 
11 from fastapi import APIRouter
12 
13 app03 = APIRouter()
14 
15 """
16 第三章 路径参数与数字验证
17 """
18 
19 @app03.get('/path/parameters')
20 async def path_param01():
21     return {"msg": "this is a msg"}
22 
23 
24 @app03.get('/path/{parameters}')
25 async def path_param02(parameters: str):
26     return {"msg": parameters}
View Code

@app03.get('/path/parameters')
async def path_param01():
return {"msg": "this is a msg"}


@app03.get('/path/{parameters}')
async def path_param02(parameters: str):
return {"msg": parameters}

 

2个接口的方法都是get

2个接口的路径一个是‘/path/parameters’

另一个是'/path/{parameters}'

方法的参数和方法的返回值不同。

此时调用2个接口

调用第一个接口的结果如下:

 

 调用第二个接口的结果如下:

 

 

 

 

调换2个接口的位置顺序

 

 1 '''
 2 @author:invoker
 3 @project:fastapi202108
 4 @file: chapter03.py
 5 @contact:invoker2021@126.com
 6 @descript:
 7 @Date:2021/8/5 21:22
 8 @version: Python 3.7.8
 9 '''
10 
11 from fastapi import APIRouter
12 
13 app03 = APIRouter()
14 
15 """
16 第三章 路径参数与数字验证
17 """
18 
19 @app03.get('/path/{parameters}')
20 async def path_param02(parameters: str):
21     return {"msg": parameters}
22 
23 @app03.get('/path/parameters')
24 async def path_param01():
25     return {"msg": "this is a msg"}
View Code

@app03.get('/path/{parameters}')
async def path_param02(parameters: str):
return {"msg": parameters}

@app03.get('/path/parameters')
async def path_param01():
return {"msg": "this is a msg"}

 

重新测试执行2个接口:

调用第一个接口:

 

 

 

调用第二个接口:

 

 

 

由此可见,当2个接口的路径和调用方法相同时,使用的是第一个接口的返回值

 

posted @ 2021-08-05 22:15  kaer_invoker  阅读(354)  评论(0编辑  收藏  举报