- 一.Demo项目框架图
- 二.Demo项目代码
- 1.AopLogDemoController
- 2.IAopLogService
- 3.BaseReq
- 4.BaseResp
- 5.TestResp
- 6.AopLogServiceImpl
- 7.StatusCode
- 8.RequestAndResponseLog
- 9.pom.xml
- 三.测试Demo项目
@RestController
@RequestMapping("/aopLog")
public class AopLogDemoController {
@Autowired
private IAopLogService aopLogService;
@GetMapping("/test")
public BaseResp testLog(BaseReq req){
BaseResp resp = aopLogService.testLog(req);
return resp;
}
}
2.IAopLogService
public interface IAopLogService{
BaseResp testLog(BaseReq req);
}
3.BaseReq
public class BaseReq {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
4.BaseResp
public class BaseResp5.TestResp{ private Integer code; private String msg; private T result; public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getResult() { return result; } public void setResult(T result) { this.result = result; } }
public class TestResp {
private Integer id;
private String name;
private String dept;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
6.AopLogServiceImpl
@Service
public class AopLogServiceImpl implements IAopLogService {
@Override
public BaseResp testLog(BaseReq req) {
BaseResp resp = new BaseResp();
TestResp testResp = new TestResp();
testResp.setId(req.getId());
testResp.setName("玛卡巴卡");
testResp.setDept("技术部");
resp.setResult(testResp);
resp.setCode(StatusCode.SUCCESS);
resp.setMsg("获取testLog数据成功");
return resp;
}
}
7.StatusCode
public class StatusCode {
public static final int SUCCESS = 0; //成功状态码
public static final int FAIL =1 ; //失败状态码
}
8.RequestAndResponseLog
@Aspect
@Component
public class RequestAndResponseLog {
private Logger logger = LoggerFactory.getLogger(RequestAndResponseLog.class);
@Pointcut("execution(* com.aop.log.controller.*.*(..))")
public void controllerPointCut() {
}
@Before("controllerPointCut()")
public void logControllerParam(JoinPoint joinPoint) {
//获取方法、方法名
String className = joinPoint.getSignature().getDeclaringType().getSimpleName();
String methodName = joinPoint.getSignature().getName();
String method = className + "." + methodName;
//获取参数
Object[] args = joinPoint.getArgs();
//打印日志
logger.info("请求方法:{},请求参数:{}",method, JSONObject.toJSONString(args));
}
@AfterReturning(pointcut = "controllerPointCut()", returning = "result")
public void doAfterReturning(JoinPoint joinPoint,BaseResp result) {
String className = joinPoint.getSignature().getDeclaringType().getSimpleName();
String methodName = joinPoint.getSignature().getName();
String method = className + "." + methodName;
logger.info("请求方法:{},返回值:{}",method,JSONObject.toJSONString(result));
}
}
9.pom.xml
三.测试Demo项目4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.1 com.aop log 0.0.1-SNAPSHOT log Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-aop com.alibaba fastjson 2.0.8 org.springframework.boot spring-boot-maven-plugin
1.通过Postman发起请求:
2.控制台打印出请求参数和返回值日志
如有不对之处 敬请指正



