spring Boot使用AOP统一处理Web请求日志记录
1.使用spring boot实现一个拦截器
1、引入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>/**
* 拦截器:记录O2O调用接口记录
*/
@Aspect
@Component
public class O2oInterfaceInterceptor {
@Autowired
private SysCallInterfaceLogRepository sysCallInterfaceLogRepository;
@Autowired
private SysEmailService sysEmailService;
@Autowired
private SysPropertyService sysPropertyService;
private static Logger logger = LoggerFactory.getLogger(O2oInterfaceInterceptor.class);
/**
* 定义拦截规则:拦截com.ctop.wms.interfaces.ActivitiInterfaces包下面的所有类中,有@RequestMapping注解的方法。
*/
@Pointcut("execution(* com.ctop.wms.interfaces.ActivitiInterfaces.*(..)) and @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void controllerMethodPointcut() {}
@Around("controllerMethodPointcut()")
public Object controllerMethodPointcutInterceptor(ProceedingJoinPoint pjp) {
return Interceptor(pjp);
}
/**
* 拦截器具体实现
* @param pjp
* @return JsonResult(被拦截方法的执行结果)
*/
public Object Interceptor(ProceedingJoinPoint pjp){
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod(); //获取被拦截的方法
String methodName = method.getName(); //获取被拦截的方法名
String ip = request.getRemoteAddr();
String url = request.getRequestURL().toString();
logger.info("被调接口,请求开始----------------------------");
logger.info("ip : " + request.getRemoteAddr());
logger.info("url : " + request.getRequestURL().toString());
logger.info("methodName : " + methodName);
Object result = "";
String param = Arrays.toString(pjp.getArgs());
logger.info("param : " + param);
SysCallInterfaceLog log = new SysCallInterfaceLog();
log.setExt1("called");// 被叫
log.setRequestIp(ip);
log.setRequestMethod(methodName);
log.setRequestParam(param);
log.setRequestUrl(url);
try {
// 一切正常的情况下,继续执行被拦截的方法
result = pjp.proceed();
String resultStr = "";
if(result != null) {
if(result instanceof String) {
resultStr = (String) result;
} else {
Gson gson = new Gson();
resultStr = gson.toJson(result);
}
}
log.setResult(resultStr);
logger.info("result : " + resultStr);
logger.info("请求结束,请求成功");
// 记录结果到数据库
log.setSuccess("success");
log = sysCallInterfaceLogRepository.save(log);
} catch (Throwable e) {
logger.info("请求结束,请求失败");
// 记录结果到数据库, 并发送邮件
log.setSuccess("fail");
Gson gson = new Gson();
String exceptionStr = gson.toJson(e);
log.setResult(exceptionStr);
SysProperty sysPropertyName= sysPropertyService.getSysProperty("o2o.wms.log.name");
SysProperty sysPropertyEmail= sysPropertyService.getSysProperty("o2o.wms.log.email");
log = sysCallInterfaceLogRepository.save(log);
SysEmailDto sysEmailDto = new SysEmailDto();
List<SysEmailInfoDto> sysEmailInfoDtoLs = new ArrayList<SysEmailInfoDto>();
SysEmailInfoDto dtoDetails = new SysEmailInfoDto();
if(sysPropertyName !=null && sysPropertyEmail !=null){
dtoDetails.setReceiverEmail(sysPropertyEmail.getPropValue());
dtoDetails.setReceiverName(sysPropertyName.getPropValue());
}
sysEmailDto.setTitle("O2O调用WMS接口失败日志!");
sysEmailDto.setContent("调用:"+url+"失败!sys_Call_Interface_Log.Scil_Uuid="+log.getScilUuid()+",\n请求参数:"+param+",\n调用结果:"+exceptionStr);
sysEmailInfoDtoLs.add(dtoDetails);
sysEmailDto.setSysEmailInfoDto(sysEmailInfoDtoLs);
try {
sysEmailService.addSysEmail(sysEmailDto);
} catch (Exception e1) {
e1.printStackTrace();
}
e.printStackTrace();
throw new BusinessException(e, null, null);
}
return result;
}
}
spring Boot使用AOP统一处理Web请求日志记录的更多相关文章
- spring boot使用AOP统一处理web请求
为了保证服务的高可用,及时发现问题,迅速解决问题,为应用添加log是必不可少的. 但是随着项目的增大,方法增多,每个方法加单独加日志处理会有很多冗余 那在SpringBoot项目中如何统一的处理Web ...
- 46. Spring Boot中使用AOP统一处理Web请求日志
在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...
- Springboot中使用AOP统一处理Web请求日志
title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...
- SpringBoot2.0 使用AOP统一处理Web请求日志(完整版)
一,加入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- (转)Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- 转:Spring Boot中使用AOP统一处理Web请求日志
在spring boot中,简单几步,使用spring AOP实现一个拦截器: 1.引入依赖: <dependency> <groupId>org.springframewor ...
- Spring Boot2.0之统一处理web请求日志
试问,你的项目中,如果有几万个方法,你还这么写log.info("name"+name+",age"+age )日志么?low~ 所以用AOP呀 1.首先创建个 ...
- springboot Aop 统一处理Web请求日志
1.增加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
随机推荐
- selenium(四)操作cookie,伪造cookie
简介: Cookie,有时也用其复数形式 Cookies,指某些网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上的数据. 常见的用途就是保留用户登陆信息,登陆时的7天免登陆,记住 ...
- C中预编译详解
预处理过程扫描源代码,对其进行初步的转换,产生新的源代码提供给编译器.可见预处理过程先于编译器对源代码进行处理.在C 语言中,并没有任何内在的机制来完成如下一些功能:在编译时包含其他源文件.定义宏.根 ...
- DIY微型操作系统(1)—— 开发的准备
这个连载是根据<30天自制操作系统>这本书所写 只是类似于补充之类的东西,要详细的讲解,还请参照书上的内容 所以,首先我们要感谢作者川合秀实先生!(鞠躬) 为什么我想写这么一个补充的? 因 ...
- ios数据持久化(转)
文件系统 归档和序列化 数据库 1.文件系统 不管是Mac OS X 还是iOS的文件系统都是建立在UNIX文件系统基础之上的. 1.1 沙盒模型 在iOS中,一个App的读写权限只局限于自己的沙盒目 ...
- git add && git add -u && git add -A
git add将当前工作目录中更改或者新增的文件加入到Git的索引中,加入到Git的索引中就表示记入了版本历史中,这也是提交之前所需要执行的一步.可以递归添加,即如果后面跟的是一个目录作为参数,则会递 ...
- 添加react-router
1.index.js 内容: import React from 'react' import ReactDOM from 'react-dom' import { renderRoutes } fr ...
- HDU 1254 推箱子(BFS)
Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不 ...
- C语言基础:分支语句和常见运算符 分类: iOS学习 c语言基础 2015-06-10 21:44 13人阅读 评论(0) 收藏
if(判断条件){ 执行语句; }else if(判断条件){ 执行语句; } switch (整型表达式){ case 值1: 执行语句; break; case 值2: 执行语句; break; ...
- ElasticSearch(八):springboot集成ElasticSearch集群并使用
1. 集群的搭建 见:ElasticSearch(七) 2. springboot配置集群 2.1 创建springboot项目,使用idea创建,不过多介绍(创建项目时候建议不要勾选elastics ...
- GridView实现数据编辑和删除
<asp:GridView ID="gv_Emplogin" runat="server" AutoGenerateColumns="False ...