From 465880fd8175569a86ef1feb38471198c5402e39 Mon Sep 17 00:00:00 2001 From: dashan Date: Tue, 29 Aug 2023 23:13:46 +0800 Subject: [PATCH] =?UTF-8?q?config:=E6=B7=BB=E5=8A=A0web-config=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=A8=A1=E5=9D=97=EF=BC=8C=E5=B9=B6=E4=B8=94=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=85=AC=E5=85=B1swagger=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 3 + .idea/encodings.xml | 3 + .../com/ds/miniapps/biz/DsWebInterceptor.java | 48 +++++++++++++++ .../ds/miniapps/biz/MiniAppsApplication.java | 3 + .../biz/api/dummyinfo/DummyInfoApi.java | 36 ++++++++++++ .../service/dummyinfo/DummyInfoService.java | 4 +- .../impl/dummyinfo/DummyInfoServiceImpl.java | 4 +- .../src/main/resources/application.yml | 18 ++++++ .../model/entity/dummyinfo/DummyInfo.java | 13 ++--- ds-biz/pom.xml | 18 ++---- ds-commons/ds-config/ds-web-config/pom.xml | 58 +++++++++++++++++++ .../web/config/swagger/SwaggerConfig.java | 38 ++++++++++++ .../web/config/swagger/SwaggerYml.java | 44 ++++++++++++++ ds-commons/ds-config/pom.xml | 24 ++++++++ ds-commons/pom.xml | 1 + pom.xml | 21 ++++--- 16 files changed, 300 insertions(+), 36 deletions(-) create mode 100644 ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/DsWebInterceptor.java create mode 100644 ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/api/dummyinfo/DummyInfoApi.java create mode 100644 ds-biz/ds-miniapps/ds-miniapps-biz/src/main/resources/application.yml create mode 100644 ds-commons/ds-config/ds-web-config/pom.xml create mode 100644 ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/swagger/SwaggerConfig.java create mode 100644 ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/swagger/SwaggerYml.java create mode 100644 ds-commons/ds-config/pom.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml index f2b9a75..43f0835 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -10,6 +10,7 @@ + @@ -23,6 +24,7 @@ + @@ -30,6 +32,7 @@ + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml index d7cb999..7d38b99 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -9,6 +9,9 @@ + + + diff --git a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/DsWebInterceptor.java b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/DsWebInterceptor.java new file mode 100644 index 0000000..fdf2868 --- /dev/null +++ b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/DsWebInterceptor.java @@ -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 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中的局部变量 + } + } +} diff --git a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java index e55a192..6e824c9 100644 --- a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java +++ b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java @@ -1,13 +1,16 @@ package com.ds.miniapps.biz; +import com.ds.commons.web.config.swagger.SwaggerConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Import; /** * @author ds * @since 2023/8/27 */ @SpringBootApplication +@Import(SwaggerConfig.class) public class MiniAppsApplication { public static void main(String[] args) { diff --git a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/api/dummyinfo/DummyInfoApi.java b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/api/dummyinfo/DummyInfoApi.java new file mode 100644 index 0000000..64ca084 --- /dev/null +++ b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/api/dummyinfo/DummyInfoApi.java @@ -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 getDummyInfoList( + @RequestParam("num") @Min(message = "数量不能小于1",value = 1) Integer num + ){ + return dummyInfoService.randCreateDummyInfo(num); + } +} diff --git a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/service/dummyinfo/DummyInfoService.java b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/service/dummyinfo/DummyInfoService.java index d6e83a9..c08cb59 100644 --- a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/service/dummyinfo/DummyInfoService.java +++ b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/service/dummyinfo/DummyInfoService.java @@ -1,8 +1,8 @@ package com.ds.miniapps.biz.service.dummyinfo; import com.ds.miniapps.model.entity.dummyinfo.DummyInfo; +import jakarta.validation.constraints.NotNull; -import javax.validation.constraints.NotNull; import java.util.List; /** @@ -15,5 +15,5 @@ public interface DummyInfoService { * @param num 数量 * @return 假人集合 */ - List randCreateDummyInfo(@NotNull(message = "数量不能为空")Integer num); + List randCreateDummyInfo(@NotNull(message = "数量不允许为空") Integer num); } diff --git a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/service/impl/dummyinfo/DummyInfoServiceImpl.java b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/service/impl/dummyinfo/DummyInfoServiceImpl.java index c70d237..6ba592e 100644 --- a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/service/impl/dummyinfo/DummyInfoServiceImpl.java +++ b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/service/impl/dummyinfo/DummyInfoServiceImpl.java @@ -5,9 +5,9 @@ import com.ds.commons.utils.stringutil.RandInitPhoneNumbersUtils; import com.ds.miniapps.biz.service.dummyinfo.DummyInfoService; import com.ds.miniapps.model.entity.dummyinfo.DummyInfo; import com.ds.miniapps.model.enums.dummyinfo.TelecomOperatorsEnum; +import jakarta.validation.constraints.NotNull; import org.springframework.stereotype.Service; -import javax.validation.constraints.NotNull; import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -19,7 +19,7 @@ import java.util.Random; @Service public class DummyInfoServiceImpl implements DummyInfoService { @Override - public List randCreateDummyInfo(@NotNull(message = "数量不能为空") Integer num) { + public List randCreateDummyInfo(@NotNull(message = "数量不允许为空") Integer num) { if (num>1000){ num=1000; } diff --git a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/resources/application.yml b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/resources/application.yml new file mode 100644 index 0000000..53a50d2 --- /dev/null +++ b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/resources/application.yml @@ -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} \ No newline at end of file diff --git a/ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/model/entity/dummyinfo/DummyInfo.java b/ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/model/entity/dummyinfo/DummyInfo.java index 0e48024..0c77b5f 100644 --- a/ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/model/entity/dummyinfo/DummyInfo.java +++ b/ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/model/entity/dummyinfo/DummyInfo.java @@ -3,8 +3,7 @@ package com.ds.miniapps.model.entity.dummyinfo; import com.alibaba.excel.annotation.ExcelProperty; import com.ds.miniapps.model.enums.dummyinfo.TelecomOperatorsConvert; import com.ds.miniapps.model.enums.dummyinfo.TelecomOperatorsEnum; -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; /** @@ -12,20 +11,20 @@ import lombok.Data; * @since 2023/8/27 */ @Data -@ApiModel("假人信息类") +@Schema(name = "假人信息类") public class DummyInfo { - @ApiModelProperty("姓名") + @Schema(name = "姓名") private String name; - @ApiModelProperty("手机号") + @Schema(name = "手机号") private String phone; - @ApiModelProperty("运营商信息") + @Schema(name = "运营商信息") @ExcelProperty(converter = TelecomOperatorsConvert.class) private TelecomOperatorsEnum telecomOperator; - @ApiModelProperty("性别") + @Schema(name = "性别") private String sex; } diff --git a/ds-biz/pom.xml b/ds-biz/pom.xml index 72eba32..711a015 100644 --- a/ds-biz/pom.xml +++ b/ds-biz/pom.xml @@ -26,25 +26,15 @@ org.projectlombok lombok - - org.springframework.boot - spring-boot-starter-web - org.springframework.boot spring-boot-starter-test + - io.springfox - springfox-boot-starter - - - com.github.xiaoymin - knife4j-spring-boot-starter - - - com.github.xiaoymin - swagger-bootstrap-ui + com.ds.common.web.config + ds-web-config + 0.0.1-SNAPSHOT com.alibaba diff --git a/ds-commons/ds-config/ds-web-config/pom.xml b/ds-commons/ds-config/ds-web-config/pom.xml new file mode 100644 index 0000000..48f094c --- /dev/null +++ b/ds-commons/ds-config/ds-web-config/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.ds + ds-config + 0.0.1-SNAPSHOT + + + com.ds.common.web.config + ds-web-config + + + 17 + 17 + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.hibernate.validator + hibernate-validator + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + + + org.springdoc + springdoc-openapi-starter-webmvc-api + + + com.github.xiaoymin + knife4j-openapi3-spring-boot-starter + + + + org.projectlombok + lombok + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + \ No newline at end of file diff --git a/ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/swagger/SwaggerConfig.java b/ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/swagger/SwaggerConfig.java new file mode 100644 index 0000000..34e5395 --- /dev/null +++ b/ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/swagger/SwaggerConfig.java @@ -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()); + } +} \ No newline at end of file diff --git a/ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/swagger/SwaggerYml.java b/ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/swagger/SwaggerYml.java new file mode 100644 index 0000000..22794a4 --- /dev/null +++ b/ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/swagger/SwaggerYml.java @@ -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); + } +} diff --git a/ds-commons/ds-config/pom.xml b/ds-commons/ds-config/pom.xml new file mode 100644 index 0000000..f180bc2 --- /dev/null +++ b/ds-commons/ds-config/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + com.ds + ds-commons + 0.0.1-SNAPSHOT + + + ds-config + pom + + ds-web-config + + + + 17 + 17 + UTF-8 + + + \ No newline at end of file diff --git a/ds-commons/pom.xml b/ds-commons/pom.xml index b130266..41e31dc 100644 --- a/ds-commons/pom.xml +++ b/ds-commons/pom.xml @@ -14,6 +14,7 @@ ds-utils ds-basemodel + ds-config diff --git a/pom.xml b/pom.xml index 21e0259..df54a1c 100644 --- a/pom.xml +++ b/pom.xml @@ -26,9 +26,8 @@ 3.1.0 2022.0.2 2022.0.0.0-RC2 - 3.0.3 - 1.8.5 - 3.0.0 + 2.1.0 + 4.3.0 @@ -55,19 +54,19 @@ import - io.springfox - springfox-boot-starter - ${springfox.version} + org.springdoc + springdoc-openapi-starter-webmvc-ui + ${springdoc.version} - com.github.xiaoymin - knife4j-spring-boot-starter - ${knife4j.version} + org.springdoc + springdoc-openapi-starter-webmvc-api + ${springdoc.version} com.github.xiaoymin - swagger-bootstrap-ui - ${swagger-bootstrap-ui.version} + knife4j-openapi3-spring-boot-starter + ${knife4j.version}