dashan
1 year ago
16 changed files with 300 additions and 36 deletions
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
package com.ds.miniapps.biz; |
||||
|
||||
import jakarta.servlet.http.HttpServletRequest; |
||||
import jakarta.servlet.http.HttpServletResponse; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.web.servlet.HandlerInterceptor; |
||||
import org.springframework.web.servlet.ModelAndView; |
||||
|
||||
/** |
||||
* @author ds |
||||
* @since 2023/8/29 |
||||
*/ |
||||
@Slf4j |
||||
public class DsWebInterceptor implements HandlerInterceptor { |
||||
|
||||
private static final ThreadLocal<Long> START_THREAD_LOCAL = new ThreadLocal<>(); |
||||
|
||||
@Override |
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) |
||||
throws Exception { |
||||
String uri = request.getRequestURI(); |
||||
log.info(uri + " preHandle"); |
||||
Long startTime = System.currentTimeMillis(); //获取开始时间
|
||||
START_THREAD_LOCAL.set(startTime); //线程绑定变量(该数据只有当前请求的线程可见)
|
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, |
||||
ModelAndView modelAndView) throws Exception { |
||||
String uri = request.getRequestURI(); |
||||
log.info(uri + " postHandle"); |
||||
Long startTime = START_THREAD_LOCAL.get();//得到线程绑定的局部变量(开始时间)
|
||||
Long endTime = System.currentTimeMillis(); //2、结束时间
|
||||
Long time = endTime - startTime; |
||||
log.info("http request all time: " + time + "ms"); |
||||
} |
||||
|
||||
@Override |
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, |
||||
Exception ex) throws Exception { |
||||
String uri = request.getRequestURI(); |
||||
log.info(uri + " afterCompletion"); |
||||
if (START_THREAD_LOCAL != null) { |
||||
START_THREAD_LOCAL.remove(); // 移除ThreadLocal中的局部变量
|
||||
} |
||||
} |
||||
} |
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
package com.ds.miniapps.biz.api.dummyinfo; |
||||
|
||||
import com.ds.miniapps.biz.service.dummyinfo.DummyInfoService; |
||||
import com.ds.miniapps.model.entity.dummyinfo.DummyInfo; |
||||
import io.swagger.v3.oas.annotations.Operation; |
||||
import io.swagger.v3.oas.annotations.tags.Tag; |
||||
import jakarta.validation.constraints.Min; |
||||
import lombok.AllArgsConstructor; |
||||
import org.springframework.validation.annotation.Validated; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestParam; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author ds |
||||
* @since 2023/8/28 |
||||
*/ |
||||
@RestController |
||||
@AllArgsConstructor |
||||
@Tag(name = "虚拟人信息") |
||||
@RequestMapping("/dummy_info") |
||||
@Validated |
||||
public class DummyInfoApi { |
||||
|
||||
private DummyInfoService dummyInfoService; |
||||
@GetMapping("/get_list") |
||||
@Operation(summary = "获取指定数量虚拟人信息") |
||||
public List<DummyInfo> getDummyInfoList( |
||||
@RequestParam("num") @Min(message = "数量不能小于1",value = 1) Integer num |
||||
){ |
||||
return dummyInfoService.randCreateDummyInfo(num); |
||||
} |
||||
} |
@ -0,0 +1,18 @@
@@ -0,0 +1,18 @@
|
||||
server: |
||||
port: 15101 |
||||
spring: |
||||
application: |
||||
name: ds-mini-apps |
||||
cloud: |
||||
nacos: |
||||
server-addr: @nacosAddress@ |
||||
username: nacos |
||||
password: nacos |
||||
discovery: |
||||
namespace: @nacosNamespace@ |
||||
config: |
||||
file-extension: yml |
||||
namespace: @nacosNamespace@ |
||||
config: |
||||
import: |
||||
- optional:nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension} |
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<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> |
||||
<parent> |
||||
<groupId>com.ds</groupId> |
||||
<artifactId>ds-config</artifactId> |
||||
<version>0.0.1-SNAPSHOT</version> |
||||
</parent> |
||||
|
||||
<groupId>com.ds.common.web.config</groupId> |
||||
<artifactId>ds-web-config</artifactId> |
||||
|
||||
<properties> |
||||
<maven.compiler.source>17</maven.compiler.source> |
||||
<maven.compiler.target>17</maven.compiler.target> |
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||
</properties> |
||||
|
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-web</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.hibernate.validator</groupId> |
||||
<artifactId>hibernate-validator</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springdoc</groupId> |
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springdoc</groupId> |
||||
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>com.github.xiaoymin</groupId> |
||||
<artifactId>knife4j-openapi3-spring-boot-starter</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.projectlombok</groupId> |
||||
<artifactId>lombok</artifactId> |
||||
</dependency> |
||||
<!--注册中心客户端--> |
||||
<dependency> |
||||
<groupId>com.alibaba.cloud</groupId> |
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> |
||||
</dependency> |
||||
<!--配置中心客户端--> |
||||
<dependency> |
||||
<groupId>com.alibaba.cloud</groupId> |
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
</project> |
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
package com.ds.commons.web.config.swagger; |
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI; |
||||
import io.swagger.v3.oas.models.info.Contact; |
||||
import io.swagger.v3.oas.models.info.Info; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Import; |
||||
|
||||
|
||||
|
||||
/** |
||||
* @author ds |
||||
* @since 2023/8/28 |
||||
*/ |
||||
@Configuration |
||||
@Import(SwaggerYml.class) |
||||
public class SwaggerConfig { |
||||
|
||||
private final SwaggerYml swaggerYml; |
||||
|
||||
public SwaggerConfig(SwaggerYml swaggerYml) { |
||||
this.swaggerYml = swaggerYml; |
||||
} |
||||
|
||||
private Info info(){ |
||||
SwaggerYml.Contact contact = swaggerYml.getContact(); |
||||
return new Info().title(swaggerYml.getTitle()) |
||||
.termsOfService(swaggerYml.getServerUrl()) |
||||
.description(swaggerYml.getDescription()) |
||||
.contact(new Contact().url(contact.getUrl()).name(contact.getName()).email(contact.getEmail())) |
||||
.version(swaggerYml.getVersion()); |
||||
} |
||||
@Bean |
||||
public OpenAPI openAPI() { |
||||
return new OpenAPI().info(info()); |
||||
} |
||||
} |
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
package com.ds.commons.web.config.swagger; |
||||
|
||||
import lombok.Data; |
||||
import org.apache.commons.lang3.builder.ToStringBuilder; |
||||
import org.apache.commons.lang3.builder.ToStringStyle; |
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
/** |
||||
* @author ds |
||||
* @since 2023/8/28 |
||||
*/ |
||||
@Data |
||||
@Component |
||||
@ConfigurationProperties(prefix = "swagger") |
||||
public class SwaggerYml { |
||||
private String version; |
||||
|
||||
private String title; |
||||
|
||||
private String description; |
||||
|
||||
private String serverUrl; |
||||
|
||||
private Contact contact; |
||||
|
||||
|
||||
@Data |
||||
public static class Contact{ |
||||
private String url; |
||||
private String email; |
||||
private String name; |
||||
} |
||||
|
||||
|
||||
public SwaggerYml getInstance(){ |
||||
return this; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); |
||||
} |
||||
} |
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<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> |
||||
<parent> |
||||
<groupId>com.ds</groupId> |
||||
<artifactId>ds-commons</artifactId> |
||||
<version>0.0.1-SNAPSHOT</version> |
||||
</parent> |
||||
|
||||
<artifactId>ds-config</artifactId> |
||||
<packaging>pom</packaging> |
||||
<modules> |
||||
<module>ds-web-config</module> |
||||
</modules> |
||||
|
||||
<properties> |
||||
<maven.compiler.source>17</maven.compiler.source> |
||||
<maven.compiler.target>17</maven.compiler.target> |
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||
</properties> |
||||
|
||||
</project> |
Loading…
Reference in new issue