像SpringBoot一样使用Flask - 4.拦截器

接上文《像SpringBoot一样使用Flask - 3.蓝图路由Blueprint》,我们已经整理了一个干净的"启动类",现在要加入一些拦截器,为了方便统一管理。

一、常用的拦截器

python 复制代码
# 拦截器
@app.before_request
def handle_before_request():
    """在每次请求之前都被执行"""
    print("handle_before_request 被执行")


@app.after_request
def handle_after_request(response):
    """在每次请求(视图函数处理)之后都被执行, 前提是视图函数没有出现异常"""
    print("handle_after_request 被执行")
    return response


@app.errorhandler(Exception)
def error_handler(e):
    """
    全局异常捕获
    """
    print(str(e))

二、针对蓝图的一些拦截器,有没有很像@ControllerAdvice???

python 复制代码
@test_bp.before_request

@test_bp.after_request

三、针对统一管理,我们将继续前进,例如日志?统一报文?统一异常处理,或则你还有其他想要实现的,发送消息告诉我吧~

相关推荐
moxiaoran57535 分钟前
Go语言的错误处理
开发语言·后端·golang
Coder_Boy_2 小时前
基于SpringAI的在线考试系统-DDD业务领域模块设计思路
java·数据库·人工智能·spring boot·ddd
Voyager_45 小时前
StringRedisTemplate 和 RedisTemplate 的区别是什么?
java·spring boot
短剑重铸之日6 小时前
《7天学会Redis》特别篇: Redis分布式锁
java·redis·分布式·后端·缓存·redission·看门狗机制
小北方城市网6 小时前
SpringBoot 全局异常处理与接口规范实战:打造健壮可维护接口
java·spring boot·redis·后端·python·spring·缓存
Chan166 小时前
【 微服务SpringCloud | 方案设计 】
java·spring boot·微服务·云原生·架构·intellij-idea
hanqunfeng7 小时前
(三十三)Redisson 实战
java·spring boot·后端
计算机毕设指导67 小时前
基于微信小程序的运动场馆服务系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
曲幽7 小时前
Django入门指南:Python Web开发的“瑞士军刀”
python·django·flask·fastapi·web·pythonweb
小北方城市网7 小时前
SpringBoot 集成 MyBatis-Plus 实战(高效 CRUD 与复杂查询):简化数据库操作
java·数据库·人工智能·spring boot·后端·安全·mybatis