瀏覽代碼

feat: 增加文件工具类、Http工具类,全局日志记录机制

ChenSiTong 6 年之前
父節點
當前提交
a104ddf3bb

+ 57 - 0
src/main/java/nju/seec/helper/aop/log/LogAspect.java

@@ -0,0 +1,57 @@
+package nju.seec.helper.aop.log;
+
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.AfterThrowing;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+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;
+
+/**
+ * @author cst
+ */
+@Component
+@Slf4j
+@Aspect
+public class LogAspect {
+    @Pointcut("execution(public * nju.seec.helper.web.controller..*.*(..))")
+    public void controllerPointcut() {
+
+    }
+
+    @SneakyThrows
+    @Around("controllerPointcut()")
+    public Object aroundController(ProceedingJoinPoint joinPoint) {
+        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
+                RequestContextHolder.getRequestAttributes();
+
+        if (requestAttributes != null) {
+            HttpServletRequest request = requestAttributes.getRequest();
+            log.info("IP地址 " + request.getRemoteAddr() + " 访问 " +
+                    joinPoint.getTarget().getClass().getName() + " 类 " +
+                    joinPoint.getSignature().getName() + " 方法");
+        }
+
+        return joinPoint.proceed();
+    }
+
+    @Pointcut("execution(public * nju.seec.helper.logic.service.impl..*.*(..))")
+    public void servicePointcut() {
+
+    }
+
+    @AfterThrowing(pointcut = "servicePointcut()", throwing = "e")
+    public void afterServiceThrowing(JoinPoint joinPoint, Throwable e) {
+        log.error(joinPoint.getTarget().getClass().getName() + " 类 " +
+                joinPoint.getSignature().getName() + " 方法出现异常:" +
+                e.getLocalizedMessage()
+        );
+    }
+}

+ 16 - 0
src/main/java/nju/seec/helper/util/FileUtils.java

@@ -0,0 +1,16 @@
+package nju.seec.helper.util;
+
+import lombok.experimental.UtilityClass;
+
+import java.io.File;
+import java.nio.file.Paths;
+
+/**
+ * @author cst
+ */
+@UtilityClass
+public class FileUtils {
+    public File getFile(String path) {
+        return Paths.get(path).toFile();
+    }
+}

+ 28 - 0
src/main/java/nju/seec/helper/util/HttpUtils.java

@@ -0,0 +1,28 @@
+package nju.seec.helper.util;
+
+import lombok.experimental.UtilityClass;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * @author cst
+ */
+@UtilityClass
+public class HttpUtils {
+    private static final RestTemplate REST_TEMPLATE = new RestTemplate();
+
+    public <T> T get(String url, Class<T> responseType, Object... uriVariables) {
+        return REST_TEMPLATE.getForObject(url, responseType, uriVariables);
+    }
+
+    public <T> T post(String url, Object request, Class<T> responseType, Object... uriVariables) {
+        return REST_TEMPLATE.postForObject(url, request, responseType, uriVariables);
+    }
+
+    public void put(String url, Object request, Object... uriVariables) {
+        REST_TEMPLATE.put(url, request, uriVariables);
+    }
+
+    public void delete(String url, Object... uriVariables) {
+        REST_TEMPLATE.delete(url, uriVariables);
+    }
+}