Springboot AOP 拦截 直接返回结果数据

简介: Springboot AOP 拦截 直接返回结果数据

示例代码:


    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = attributes.getResponse();
        //校验
        Boolean statusCheck = XXXXXStatusCheck(response);
        if (!statusCheck) {
            Result<Boolean> result = new Result<>();
            result.setData(false);
            result.setSuccess(false);
            result.setResultCode(ResultCode.XXXXX_NOT_CONFIG);
            returnData(result,response);
        }
        return joinPoint.proceed();
    }
    /**
     * 返回数据
     *
     * @param result
     * @param response
     * @throws IOException
     */
    public void returnData(Result<Boolean> result, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        //这里传提示语可以改成自己项目的返回数据封装的类
        response.getWriter().println(objectMapper.writeValueAsString(result));
        return;
    }


代码简析:


image.png

相关文章
|
21天前
|
Java Spring
Springboot中Aop的使用
Springboot中Aop的使用
|
21天前
|
监控 Java API
掌握 Spring Boot AOP:使用教程
Spring Boot 中的面向切面编程(AOP)为软件开发提供了一种创新方法,允许开发者将横切关注点与业务逻辑相分离。这不仅提高了代码的复用性和可维护性,而且还降低了程序内部组件之间的耦合度。下面,我们深入探讨如何在 Spring Boot 应用程序中实践 AOP,以及它为项目带来的种种益处。
|
5天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
12天前
|
前端开发 Java
干货文:SpringBoot 配置 AOP 打印请求参数和返回参数
干货文:SpringBoot 配置 AOP 打印请求参数和返回参数
26 1
|
21天前
|
缓存 前端开发 Java
SpringBoot启动后加载初始化数据
SpringBoot启动后加载初始化数据
|
21天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
3月前
|
Java 数据库连接 应用服务中间件
Spring5源码(39)-Aop事物管理简介及编程式事物实现
Spring5源码(39)-Aop事物管理简介及编程式事物实现
24 0
|
4月前
AOP&面向切面编程
AOP&面向切面编程
55 0
|
4月前
|
Java 程序员 Maven
Spring AOP入门指南:轻松掌握面向切面编程的基础知识
Spring AOP入门指南:轻松掌握面向切面编程的基础知识
|
4月前
|
数据库
AOP(面向切面编程)的基本概念和原理
AOP(面向切面编程)的基本概念和原理
82 0