使用AOP拦截所有请求并打印日志
导入依赖
<!--spring aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
切面
package com.hsh.maoduo.aspect;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.CodeSignature;
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.util.LinkedHashMap;
import java.util.Objects;
@Aspect
@Component
@Slf4j
public class ControllerAspect {
@Pointcut("execution(* com.hsh.maoduo.controller..*.*(..))")
public void controllerMethod() {
}
@Around("controllerMethod()")
public Object aroundController(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
StringBuilder logInfo = new StringBuilder();
logInfo.append(":\n");
logInfo.append("========================================= Start ==========================================\n");
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
logInfo.append("-- URL : ").append(request.getRequestURL()).append("\n");
logInfo.append("-- 请求参数 : ").append(getParams(proceedingJoinPoint)).append("\n");
Object result;
try {
result = proceedingJoinPoint.proceed();
} catch (Exception e) {
result = e.getMessage() + "\n" + e.getStackTrace()[0].toString();
}
logInfo.append("-- 返回结果 : ").append(JSON.toJSON(result)).append("\n");
logInfo.append("========================================== End ===========================================\n");
log.info(logInfo.toString());
return result;
}
public String getParams(JoinPoint joinPoint) {
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
CodeSignature signature = (CodeSignature) joinPoint.getSignature();
String[] names = signature.getParameterNames();
Object[] value = joinPoint.getArgs();
for (int i = 0; i < names.length; i++) {
map.put(names[i], value[i]);
}
return JSONObject.toJSONString(map);
}
}
实体类Student
package com.hsh.maoduo.pojo.other;
import lombok.Data;
@Data
public class Student {
private String id;
private String name;
}
测试Controller
package com.hsh.maoduo.controller;
import com.hsh.maoduo.pojo.dto.GoodsDTO;
import com.hsh.maoduo.pojo.entity.Goods;
import com.hsh.maoduo.pojo.other.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@RestController
public class TestRestController {
@GetMapping("/test")
public String test(Student student) throws Exception{
if (student.getId() == null){
throw new Exception("异常----学生id为空");
}
return "测试返回----"+student.getId()+"----"+student.getName();
}
}
测试结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/c0f26156a8cc452bb93ac84a738283a2.png#pic_center)