Skip to content

Commit

Permalink
项目初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
liboshuai01 committed Jul 30, 2024
1 parent 0135b51 commit d6dfdb0
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 66 deletions.
86 changes: 86 additions & 0 deletions excel/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.liboshuai.starlink.demo</groupId>
<artifactId>excel</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>excel</name>
<description>演示excel相关代码</description>

<properties>
<spring.boot.version>2.7.18</spring.boot.version>
<knife4j.version>4.4.0</knife4j.version>
<lombok.version>1.18.34</lombok.version>
<fastjson2.version>2.0.41</fastjson2.version>
<hutool.version>5.8.29</hutool.version>
<easyexcel.verion>3.3.4</easyexcel.verion>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!--springboot-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot-aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--springboot配置提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--knife4j-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--FastJson2-->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-extension</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!--easyExcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.verion}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.liboshuai.starlink.demo.excel;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ExcelApplication {
public static void main(String[] args) {
SpringApplication.run(ExcelApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.liboshuai.starlink.demo.excel.common.annotation;

import java.lang.annotation.*;

/**
* @Author liboshuai
* @Date 2024-01-30 10:46
* 统计耗时
*/
@Documented //用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化.Documented是一个标记注解,没有成员.
@Target(ElementType.METHOD) //指定被修饰的Annotation可以放置的位置(被修饰的目标)类,方法,属性
@Retention(RetentionPolicy.RUNTIME) //定义注解的保留策略, RetentionPolicy.RUNTIME:注解会在class字节码文件中存在,在运行时可以通过反射获取到
public @interface TakeTime {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.liboshuai.starlink.demo.excel.common.aspct;

import com.liboshuai.starlink.demo.excel.common.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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.util.Date;

@Slf4j
@Aspect
@Component
public class TakeTimeAspect {
//统计请求的处理时间
ThreadLocal<Long> startTime = new ThreadLocal<>();
ThreadLocal<Long> endTime = new ThreadLocal<>();

/**
* 带有@TakeTime注解的方法
*/
@Pointcut("@annotation(com.liboshuai.starlink.demo.excel.common.annotation.TakeTime)")
public void TakeTime() {

}

@Before("TakeTime()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 获取方法的名称
String methodName = joinPoint.getSignature().getName();
// 获取方法入参
Object[] param = joinPoint.getArgs();
StringBuilder sb = new StringBuilder();
for (Object o : param) {
sb.append(o).append(";");
}
//接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();

log.info("==================================================");
log.info("进入方法: [{}] 参数: {}", methodName, sb);
startTime.set(System.currentTimeMillis());
log.info("开始时间: [{}] ({})", new Date(startTime.get()), startTime.get());
log.info("请求URL: [{}]", request.getRequestURL().toString());
log.info("--------------------------------------------------");
}

@AfterReturning(returning = "ret", pointcut = "TakeTime()")
public void doAfterReturning(JoinPoint joinPoint, Object ret) {
//处理完请求后,返回内容
log.info("--------------------------------------------------");
log.info("方法 [{}] 返回值: {}", joinPoint.getSignature().getName(), JsonUtil.obj2JsonStr(ret));
endTime.set(System.currentTimeMillis());
log.info("结束时间: [{}]", new Date(endTime.get()));
long duration = endTime.get() - startTime.get();
log.info("执行耗时: {} ms ({} seconds)", duration, duration / 1000.0);
log.info("==================================================");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.liboshuai.starlink.demo.excel.common.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.TypeReference;

import java.util.List;
import java.util.Map;

public class JsonUtil {

/**
* author: liboshuai
* description: Java对象转换为Json字符串
*
* @param obj:
* @return java.lang.String
*/
public static String obj2JsonStr(Object obj) {
return JSON.toJSONString(obj);
}

/**
* author: liboshuai
* description: Json字符串转换为Java对象
*
* @param json:
* @return java.lang.Object
*/
public static <T> T jsonStr2Obj(String json, Class<T> cls) {
return JSON.parseObject(json, cls);
}

/**
* author: liboshuai
* description: Json字符串转换为Java对象(下划线转驼峰)
*/
public static <T> T jsonStr2ObjSnakeCase(String json, Class<T> cls) {
return JSON.parseObject(json, cls, JSONReader.Feature.SupportSmartMatch);
}

/**
* author: liboshuai
* description: Json字符串转换为指定类型的Map
*/
public static <K, V> Map<K, List<V>> jsonStr2Map(String json, Class<K> key, Class<V> value) {
return JSON.parseObject(json, new TypeReference<Map<K, List<V>>>(key, value) {
});
}

/**
* author: liboshuai
* description: json字符串转换为指定类型的List
*/
public static <T> List<T> jsonStr2List(String json, Class<T> cls) {

return JSON.parseArray(json, cls);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.liboshuai.starlink.demo.excel.controller;

import com.liboshuai.starlink.demo.excel.common.annotation.TakeTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/excel")
@Api(tags = "excel演示相关接口")
public class ExcelController {

@TakeTime
@GetMapping("ping")
@ApiOperation("ping一下")
public String ping() {
return "pong";
}
}
21 changes: 21 additions & 0 deletions excel/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
server:
port: 15000

knife4j:
enable: true
openapi:
title: excel演示相关文档
description: excel演示相关文档
email: [email protected]
concat: liboshuaiwechat
url: https://liboshuai.com
version: v4.0
license: Apache 2.0
license-url: https://stackoverflow.com/
terms-of-service-url: https://stackoverflow.com/
group:
excel:
group-name: excel
api-rule: package
api-rule-resources:
- com.liboshuai.starlink.demo.excel
16 changes: 1 addition & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,7 @@
<groupId>com.liboshuai.starlink.demo</groupId>
<artifactId>starlink-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<packaging>pom</packaging>
<name>starlink-demo</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
13 changes: 0 additions & 13 deletions src/main/java/com/liboshuai/starlink/demo/App.java

This file was deleted.

38 changes: 0 additions & 38 deletions src/test/java/com/liboshuai/starlink/demo/AppTest.java

This file was deleted.

0 comments on commit d6dfdb0

Please sign in to comment.