利用@Around通知修改Controller的返回值

自定义一个注解@OperationBtn

在切入点Controller上加上自定义注解

接下来就是重点了,AspectJ写切面类,对该Controller 1 @Component
 @Aspect
public class OperationBtnAspect { @Autowired
private AppTableOperationServiceI appTableOperationService; @Around(value = "@annotation(anno)", argNames = "anno")
public AjaxJson aroundMthod(ProceedingJoinPoint point, OperationBtn anno) throws Throwable{
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest(); String url = request.getRequestURL().toString(); // http://localhost:8080/5U/appDataController.do (URL:返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数。)
String method = request.getMethod(); // POST
String uri = request.getRequestURI(); // /5U/appDataController.do
String queryString = request.getQueryString(); // addOrUpdateData&operId=402881e8645f580501645f61202c0044&optId=402881e8645f580501645f61203d004c(返回url路径后面的查询字符串)
queryString = queryString.substring(queryString.indexOf("operId"));
queryString = queryString.substring(,queryString.indexOf("&"));
AppTableOperationEntity operationEntity = appTableOperationService.getEntity(AppTableOperationEntity.class, queryString);
AjaxJson json = (AjaxJson) point.proceed(); //执行Controller中方法,得到返回值json
if(json.isSuccess() && operationEntity !=null && operationEntity.getCallbackSucMsg() !=null){
json.setMsg(operationEntity.getCallbackSucMsg());
}else if(!json.isSuccess() && operationEntity !=null && operationEntity.getCallbackSucMsg() !=null){
json.setMsg(operationEntity.getCallbackFailMsg());
}
return json;
}
}

Controller层aop的更多相关文章

  1. spring aop 加在Controller层造成类初始化两遍

    写一个测试项目,在配置动态数据源的时候采用的AOP切面到Controller层中,根据参数判断是否切合数据源,结果发现,每次Controller层的类都会初始化两次! 后来测试发现,把切面放到Serv ...

  2. Spring Mvc 在非controller层 实现获取request对象

    一般我们在Controller层,会编写类似这样的方法 @Controller @RequestMapping(value="/detail") public class GetU ...

  3. ssh_maven之controller层开发

    我们已经完成了前两层的开发,现在 只剩下我们的controller层了,对于这一层,我们需要创建一个动作类CustomerAction,另外就是我们的strutss.xml以及我们的applicati ...

  4. 【异常处理】Springboot对Controller层方法进行统一异常处理

    Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...

  5. Controller层的方法访问标志与Spring装配与AspectJ切面处理

    最近在做AspectJ实现的日志模块,在spring配置中加入了<aop:aspectj-autoproxy/>,之后发现,只要有用到自定义注解的类,某些方法经MVC请求时就报空指针错误. ...

  6. springMVC中controller层方法中使用private和public问题

    楼主一直习惯使用public,偶尔手误也可能使用private,但是发觉也没啥区别,都能调用service层,注入bean. 后来做一个新项目时,发觉自己以前的写的部分功能报错,当时有点懵逼,,找了半 ...

  7. Springboot对Controller层方法进行统一异常处理

    Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...

  8. DAO层,Service层,Controller层、View层 的分工合作

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  9. Junit mockito 测试Controller层方法有Pageable异常

    1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...

随机推荐

  1. Python全栈之路----函数进阶----迭代器

    我们已经知道,可以直接作用于 for 循环的数据类型有以下几种: 一类是集合数据类型,如 list , tuple , dict , set ,str 等: 一类是 generator ,包括生成器和 ...

  2. LADP(Lightweight Directory Access Protocol)轻量目录访问协议~小知识

    What is LDAP and how does it work(implementation)? LDAP stands for “Lightweight Directory Access Pro ...

  3. 记一次ssh配置的锅

    我们在使用git来管理代码的时候不可避免的要用到ssh密匙,这个密匙怎么配置的百度上很多. 我这边是使用sourcetree来配合管理代码的,但是我ssh配置好了以后无论是克隆代码还是推送代码都提示我 ...

  4. python中的列表的嵌套与转换

    第一种方法:这行代码的for循环的意识,是先将matrix列表中的每行的第一个元素拿出. matrix =[[1,2,3,4],[5,6,7,8],[9,10,11,12]] transposed_r ...

  5. PythonStudy——机器语言 Machine Language

    编程语言 编程语言(programming language),是用来定义计算机程序的形式语言.它是一种被标准化的交流技巧,用来向计算机发出指令.一种计算机语言让程序员能够准确地定义计算机所需要使用的 ...

  6. Python switch(多分支选择)的实现

    Python 中没有 switch/case 语法,如果使用 if/elif/else 会出现代码过长.不清晰等问题. 而借助字典就可以实现 switch 的功能 示例: def case1(): # ...

  7. [C#]如何将 string 安全地转换为 int

    当遇到string向int类型转换时会遇到以下问题: 1.字符串非数字格式 2.字符串描述的不是int型 3.转换后越界 这些情况都需要用try catch来捕获异常并处理 安全简单的转换可以用 In ...

  8. kafka原理和实践(五)spring-kafka配置详解

    系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...

  9. Docker Images for MySQL Group Replication 5.7.14

    In this post, I will point you to Docker images for MySQL Group Replication testing. There is a new ...

  10. Windows IIS安装php

    [可参考文档:https://docs.microsoft.com/en-us/iis/application-frameworks/scenario-build-a-php-website-on-i ...