企业网站建设原则,网站建设管理典型经验,黄岛区做网站的,wordpress网站类型前言
我们在开发的过程中#xff0c;一般都需要对方法的入参进行打印#xff0c;或者Debug调试的时候我们要查看方法入参的参数是否数量和数据正确性。
一般我们需要知道请求的参数、接口路径、请求ip等
但是考虑以后项目上线BUG排查的问题#xff0c;最好的方式就是使用…前言
我们在开发的过程中一般都需要对方法的入参进行打印或者Debug调试的时候我们要查看方法入参的参数是否数量和数据正确性。
一般我们需要知道请求的参数、接口路径、请求ip等
但是考虑以后项目上线BUG排查的问题最好的方式就是使用切面的方式来记录每个方法执行时要保存的日志处理那么下面我们来实现一个使用自定义注解的方式来对每个请求的方法上进行日志存储
AOP切面对某个方法进行增强处理例如在某个方法执行前或者执行后进行操作。
案例
首先我们看一个controller接口
import lombok.extern.slf4j.Slf4j;Slf4j
RestController
RequestMapping(/user/)
public class TestController {PostMapping(getUserById)public String getUserById(RequestBody User user) {log.info(/user/getUserById params{}, user.toString());//执行代码逻辑...return 请求成功;}
}这种一般没什么问题一般我们测试的时候都可以这样来写但如果有很多的方法那么每个方法都这样写显然很是繁琐那么我们通过下面的方式来实现当进入方法前打印请求的一些信息
实现
我们自定义一个注解名为Itboy
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Retention(RetentionPolicy.RUNTIME)
Target(ElementType.METHOD)//ElementType.METHOD表示为该注解在方法上添加
public interface Itboy {
}
然后需要用到一些依赖这些是使用AOP的依赖我们提前引入一下
dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.8.14/version
/dependency
dependencygroupIdorg.aspectj/groupIdartifactIdaspectjrt/artifactIdversion1.9.19/version
/dependency然后我们定义一个切面类名为ItboyAspect 注这篇文章我使用的System打印方式如果需要保存日志换为对应的logger.info()即可。
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;Component
Aspect
Slf4j
public class ItboyAspect {//Pointcut为切入点切入到Itboy这个注解上面Pointcut(annotation(com.mayikt.demo.Itboy))public void itboyAspect() {}//Before为在进入切点之前自动执行Before中的逻辑进入之前的前提是方法上需要有我们设置的自定注解Before(itboyAspect())public void beforeItboy(JoinPoint joinPoint) {//获取本次请求ServletRequestAttributes requestAttributes (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request requestAttributes.getRequest();//获取到方法名String methodName joinPoint.getSignature().getName();System.err.println(方法 methodName () 开始);//执行时间SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);Date date new Date();String time sdf.format(date);System.err.println(时间 : time);//请求URLSystem.err.println(URL : request.getRequestURL());//请求方法System.err.println(HTTP Method : methodName);//打印controller全路径和执行方法System.err.println(Class Method : joinPoint.getSignature().getDeclaringTypeName() . methodName);//请求IPSystem.err.println(IP : request.getRemoteHost());//请求入参System.err.println(Requert params : JSON.toJSONString(joinPoint.getArgs()));}//后置通知After(itboyAspect())public void afterItboy(JoinPoint joinPoint) {//获取到方法名String methodName joinPoint.getSignature().getName();System.err.println(方法 methodName () 结束);}
}
然后我们只需要在需要保存的方法上添加Itboy注解即可 ItboyPostMapping(selectUserList)public String getUserById(RequestBody User user) {log.info(/user/selectUserList params{}, user.toString());//执行代码逻辑...return 请求成功;}最后我们来看一下效果
2023-04-02 14:21:29.033 INFO 5912 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
方法getUserById() 开始
时间 : 2023-04-02 14:21:29
URL : http://127.0.0.1:8080/user/selectUserList
HTTP Method : getUserById
Class Method : com.mayikt.demo.TestController.getUserById
IP : 127.0.0.1
Requert params : [{address:华东,age:18,id:10010,name:贾强}]
方法getUserById() 结束
2023-04-02 14:21:29.171 INFO 5912 --- [nio-8080-exec-1] com.mayikt.demo.TestController : /user/selectUserList paramsUser(id10010, name贾强, age18, address华东)总结
使用上面这种方式减轻了我们自己手动日志打印的繁琐而且配置也相对于比较简单如果有其他需求是比较频繁的操作的话我们就可以使用AOP切面的方式来完成。
肥肠好用 文章转载自: http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.kgmkl.cn.gov.cn.kgmkl.cn http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.rdmn.cn.gov.cn.rdmn.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.krtky.cn.gov.cn.krtky.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.fpczq.cn.gov.cn.fpczq.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.wrtsm.cn.gov.cn.wrtsm.cn http://www.morning.kwyq.cn.gov.cn.kwyq.cn http://www.morning.zdmrf.cn.gov.cn.zdmrf.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.hflrz.cn.gov.cn.hflrz.cn http://www.morning.rfbt.cn.gov.cn.rfbt.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.hcqpc.cn.gov.cn.hcqpc.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.qwrb.cn.gov.cn.qwrb.cn http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn http://www.morning.ybgpk.cn.gov.cn.ybgpk.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.rtbhz.cn.gov.cn.rtbhz.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.khcpx.cn.gov.cn.khcpx.cn http://www.morning.hqwxm.cn.gov.cn.hqwxm.cn http://www.morning.swyr.cn.gov.cn.swyr.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.mnwb.cn.gov.cn.mnwb.cn http://www.morning.qwdqq.cn.gov.cn.qwdqq.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.zcmpk.cn.gov.cn.zcmpk.cn http://www.morning.zlfxp.cn.gov.cn.zlfxp.cn http://www.morning.bqhlp.cn.gov.cn.bqhlp.cn http://www.morning.jwgnn.cn.gov.cn.jwgnn.cn http://www.morning.dkslm.cn.gov.cn.dkslm.cn http://www.morning.qyhcg.cn.gov.cn.qyhcg.cn http://www.morning.qqbjt.cn.gov.cn.qqbjt.cn http://www.morning.pwghp.cn.gov.cn.pwghp.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.c-ae.cn.gov.cn.c-ae.cn http://www.morning.hkysq.cn.gov.cn.hkysq.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.xfcjs.cn.gov.cn.xfcjs.cn http://www.morning.ubpsa.cn.gov.cn.ubpsa.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.rmyqj.cn.gov.cn.rmyqj.cn http://www.morning.wjrtg.cn.gov.cn.wjrtg.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.qdrrh.cn.gov.cn.qdrrh.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.eshixi.com.gov.cn.eshixi.com