From 5fc20e3d73acbe8c44a55f30f8c0c179b47e4c17 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Tue, 5 Sep 2023 01:01:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=9F=BA=E7=A1=80=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gb28181-mocking-api/.gitignore | 38 +++++++++ gb28181-mocking-api/pom.xml | 62 ++++++++++++++ .../mocking/advice/ExceptionAdvice.java | 80 +++++++++++++++++++ .../gb28181/mocking/config/SwaggerConfig.java | 53 ++++++++++++ .../gb28181/mocking/config/WebConfig.java | 22 +++++ .../interceptor/RequestInterceptor.java | 20 +++++ gb28181-mocking-service/.gitignore | 38 +++++++++ gb28181-mocking-service/pom.xml | 32 ++++++++ gb28181-mocking-starter/pom.xml | 71 +++++++++++++++- .../mocking/Gb28181MockingStarter.java | 6 +- pom.xml | 19 ++++- 11 files changed, 436 insertions(+), 5 deletions(-) create mode 100644 gb28181-mocking-api/.gitignore create mode 100644 gb28181-mocking-api/pom.xml create mode 100644 gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/advice/ExceptionAdvice.java create mode 100644 gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/config/SwaggerConfig.java create mode 100644 gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/config/WebConfig.java create mode 100644 gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/interceptor/RequestInterceptor.java create mode 100644 gb28181-mocking-service/.gitignore create mode 100644 gb28181-mocking-service/pom.xml diff --git a/gb28181-mocking-api/.gitignore b/gb28181-mocking-api/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/gb28181-mocking-api/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/gb28181-mocking-api/pom.xml b/gb28181-mocking-api/pom.xml new file mode 100644 index 0000000..4407466 --- /dev/null +++ b/gb28181-mocking-api/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + cn.skcks.docking + gb28181-mocking + 0.0.1-SNAPSHOT + + + cn.skcks.docking.gb28181 + gb28181-mocking-api + + + 17 + 17 + UTF-8 + + + + + cn.skcks.docking.gb28181 + gb28181-mocking-service + ${project.version} + + + + cn.skcks.docking.gb28181 + annotation + ${project.version} + compile + + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + ${springdoc.version} + + + + org.springdoc + springdoc-openapi-starter-common + + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + compile + + + + jakarta.servlet + jakarta.servlet-api + + + diff --git a/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/advice/ExceptionAdvice.java b/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/advice/ExceptionAdvice.java new file mode 100644 index 0000000..161c6ed --- /dev/null +++ b/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/advice/ExceptionAdvice.java @@ -0,0 +1,80 @@ +package cn.skcks.docking.gb28181.mocking.advice; + +import cn.skcks.docking.gb28181.common.json.JsonResponse; +import jakarta.validation.ConstraintViolationException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.validation.BindException; +import org.springframework.web.HttpMediaTypeNotAcceptableException; +import org.springframework.web.HttpMediaTypeNotSupportedException; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.Objects; + +/** + * 全局异常处理类 + * + * @author Shikong + */ +@Slf4j +@RestControllerAdvice +public class ExceptionAdvice { + + @ExceptionHandler(MissingServletRequestParameterException.class) + public JsonResponse missingServletRequestParameterException(MissingServletRequestParameterException e) { + return JsonResponse.error(e.getMessage()); + } + + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public JsonResponse httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){ + return JsonResponse.error(e.getMessage()); + } + + @ExceptionHandler(HttpMediaTypeNotSupportedException.class) + public JsonResponse unsupportedMediaTypeException(Exception e) { + e.printStackTrace(); + return JsonResponse.error(e.getMessage()); + } + + @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) + public JsonResponse httpMediaTypeNotAcceptableException(HttpMediaTypeNotAcceptableException e){ + return JsonResponse.error(e.getMessage()); + } + + @ExceptionHandler(RuntimeException.class) + public JsonResponse runtimeException(RuntimeException e) { + e.printStackTrace(); + return JsonResponse.error(e.getMessage()); + } + + @ExceptionHandler(BindException.class) + public JsonResponse handleValidationBindException(BindException e) { + return JsonResponse.error(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage()); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + public JsonResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { + return JsonResponse.error(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage()); + } + + @ExceptionHandler(ConstraintViolationException.class) + public JsonResponse handleConstraintViolationException(ConstraintViolationException e) { + return JsonResponse.error(Objects.requireNonNull(e.getMessage())); + } + + @ExceptionHandler(HttpMessageNotReadableException.class) + public JsonResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e){ + log.warn("{}", e.getMessage()); + return JsonResponse.error("参数异常"); + } + + @ExceptionHandler(Exception.class) + public JsonResponse exception(Exception e) { + e.printStackTrace(); + return JsonResponse.error(e.getMessage()); + } +} diff --git a/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/config/SwaggerConfig.java b/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/config/SwaggerConfig.java new file mode 100644 index 0000000..20f2a97 --- /dev/null +++ b/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/config/SwaggerConfig.java @@ -0,0 +1,53 @@ +package cn.skcks.docking.gb28181.mocking.config; + +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +import io.swagger.v3.oas.models.security.SecurityRequirement; +import io.swagger.v3.oas.models.security.SecurityScheme; +import lombok.Data; +import org.springdoc.core.customizers.OpenApiCustomizer; +import org.springdoc.core.models.GroupedOpenApi; +import org.springframework.context.annotation.Configuration; + +import static org.springdoc.core.utils.Constants.ALL_PATTERN; + + +@Configuration +@Data +public class SwaggerConfig { + public static final Info INFO; + private static final License LICENSE; + + static { + INFO = new Info() + .title("GB28181 Docking Platform API") + .description("Matrix API description") + .version("2.0"); + LICENSE = new License() + .name("Apache 2.0") + .url("http://www.apache.org/licenses/LICENSE-2.0.html"); + INFO.license(LICENSE); + } + + public static GroupedOpenApi api(String group, String path) { + return GroupedOpenApi.builder() + .group(group) + .pathsToMatch(path + ALL_PATTERN) + .addOpenApiCustomizer(openApi -> openApi.info(SwaggerConfig.INFO)) + .addOpenApiCustomizer(authOpenApiDocs()) + .build(); + } + + private static OpenApiCustomizer authOpenApiDocs(){ + return openApi -> { + // swagger 全局添加 token 认证选项 + openApi.addSecurityItem(new SecurityRequirement().addList("token")); + openApi.getComponents() + .addSecuritySchemes("token", + new SecurityScheme() + .type(SecurityScheme.Type.APIKEY) + .in(SecurityScheme.In.HEADER) + .name("token")); + }; + } +} diff --git a/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/config/WebConfig.java b/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/config/WebConfig.java new file mode 100644 index 0000000..cbdc3bd --- /dev/null +++ b/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/config/WebConfig.java @@ -0,0 +1,22 @@ +package cn.skcks.docking.gb28181.mocking.config; + +import cn.skcks.docking.gb28181.mocking.interceptor.RequestInterceptor; +import jakarta.validation.constraints.NotNull; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Slf4j +@Configuration +@RequiredArgsConstructor +public class WebConfig implements WebMvcConfigurer { + private final RequestInterceptor requestInterceptor; + @Override + public void addInterceptors(@NotNull InterceptorRegistry registry) { + registry.addInterceptor(requestInterceptor) + .excludePathPatterns("/swagger-ui/**","/v3/api-docs/**") + .addPathPatterns("/**"); + } +} diff --git a/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/interceptor/RequestInterceptor.java b/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/interceptor/RequestInterceptor.java new file mode 100644 index 0000000..b78d6d9 --- /dev/null +++ b/gb28181-mocking-api/src/main/java/cn/skcks/docking/gb28181/mocking/interceptor/RequestInterceptor.java @@ -0,0 +1,20 @@ +package cn.skcks.docking.gb28181.mocking.interceptor; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +@Slf4j +@Component +@SuppressWarnings({"unused"}) +@RequiredArgsConstructor +public class RequestInterceptor implements HandlerInterceptor { + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + log.info("{} 访问 {}",request.getRemoteHost(), request.getRequestURI()); + return true; + } +} diff --git a/gb28181-mocking-service/.gitignore b/gb28181-mocking-service/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/gb28181-mocking-service/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/gb28181-mocking-service/pom.xml b/gb28181-mocking-service/pom.xml new file mode 100644 index 0000000..26e87e8 --- /dev/null +++ b/gb28181-mocking-service/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + cn.skcks.docking + gb28181-mocking + 0.0.1-SNAPSHOT + + + cn.skcks.docking.gb28181 + gb28181-mocking-service + + + 17 + 17 + UTF-8 + + + + + cn.skcks.docking.gb28181 + common + + + + cn.skcks.docking + zlmediakit-service + + + diff --git a/gb28181-mocking-starter/pom.xml b/gb28181-mocking-starter/pom.xml index 78e4d7e..c2a82a4 100644 --- a/gb28181-mocking-starter/pom.xml +++ b/gb28181-mocking-starter/pom.xml @@ -24,8 +24,73 @@ cn.skcks.docking.gb28181 - starter - 0.0.1-SNAPSHOT + gb28181-mocking-api + ${project.version} + + + + cn.skcks.docking.gb28181 + gb28181-mocking-service + ${project.version} + + + + cn.skcks.docking.gb28181 + common + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-undertow + ${springboot.version} + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + com.mysql + mysql-connector-j + runtime + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.junit.jupiter + junit-jupiter-api + test @@ -91,4 +156,4 @@ - \ No newline at end of file + diff --git a/gb28181-mocking-starter/src/main/java/cn/skcks/docking/gb28181/mocking/Gb28181MockingStarter.java b/gb28181-mocking-starter/src/main/java/cn/skcks/docking/gb28181/mocking/Gb28181MockingStarter.java index aa1296e..20fb34c 100644 --- a/gb28181-mocking-starter/src/main/java/cn/skcks/docking/gb28181/mocking/Gb28181MockingStarter.java +++ b/gb28181-mocking-starter/src/main/java/cn/skcks/docking/gb28181/mocking/Gb28181MockingStarter.java @@ -7,7 +7,11 @@ import org.springframework.context.annotation.ComponentScan; @EnableFeignClients(basePackages = "cn.skcks.docking.gb28181.media") @SpringBootApplication -@ComponentScan(basePackages = "cn.skcks.docking.gb28181") +@ComponentScan(basePackages = { + "cn.skcks.docking.gb28181.annotation", + "cn.skcks.docking.gb28181.common", + "cn.skcks.docking.gb28181.mocking", +}) public class Gb28181MockingStarter { public static void main(String[] args) { SpringApplication.run(Gb28181MockingStarter.class, args); diff --git a/pom.xml b/pom.xml index a08846c..d275f6f 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,8 @@ gb28181-mocking-starter gb28181-mocking-orm + gb28181-mocking-service + gb28181-mocking-api @@ -54,6 +56,8 @@ 1.4.13 + + 0.0.1-SNAPSHOT @@ -100,9 +104,22 @@ cn.skcks.docking gb28181 - 0.0.1-SNAPSHOT + ${gb28181.docking.version} pom + + + cn.skcks.docking.gb28181 + common + ${gb28181.docking.version} + + + + cn.skcks.docking + zlmediakit-service + ${gb28181.docking.version} + + org.mapstruct