项目基础结构完善

This commit is contained in:
shikong 2023-09-05 01:01:17 +08:00
parent baa6ad755a
commit 5fc20e3d73
11 changed files with 436 additions and 5 deletions

38
gb28181-mocking-api/.gitignore vendored Normal file
View File

@ -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

View File

@ -0,0 +1,62 @@
<?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>cn.skcks.docking</groupId>
<artifactId>gb28181-mocking</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.skcks.docking.gb28181</groupId>
<artifactId>gb28181-mocking-api</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>cn.skcks.docking.gb28181</groupId>
<artifactId>gb28181-mocking-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>cn.skcks.docking.gb28181</groupId>
<artifactId>annotation</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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<String> missingServletRequestParameterException(MissingServletRequestParameterException e) {
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public JsonResponse<String> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public JsonResponse<String> unsupportedMediaTypeException(Exception e) {
e.printStackTrace();
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
public JsonResponse<String> httpMediaTypeNotAcceptableException(HttpMediaTypeNotAcceptableException e){
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(RuntimeException.class)
public JsonResponse<String> runtimeException(RuntimeException e) {
e.printStackTrace();
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(BindException.class)
public JsonResponse<String> handleValidationBindException(BindException e) {
return JsonResponse.error(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public JsonResponse<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
return JsonResponse.error(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
}
@ExceptionHandler(ConstraintViolationException.class)
public JsonResponse<String> handleConstraintViolationException(ConstraintViolationException e) {
return JsonResponse.error(Objects.requireNonNull(e.getMessage()));
}
@ExceptionHandler(HttpMessageNotReadableException.class)
public JsonResponse<String> handleHttpMessageNotReadableException(HttpMessageNotReadableException e){
log.warn("{}", e.getMessage());
return JsonResponse.error("参数异常");
}
@ExceptionHandler(Exception.class)
public JsonResponse<String> exception(Exception e) {
e.printStackTrace();
return JsonResponse.error(e.getMessage());
}
}

View File

@ -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"));
};
}
}

View File

@ -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("/**");
}
}

View File

@ -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;
}
}

38
gb28181-mocking-service/.gitignore vendored Normal file
View File

@ -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

View File

@ -0,0 +1,32 @@
<?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>cn.skcks.docking</groupId>
<artifactId>gb28181-mocking</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.skcks.docking.gb28181</groupId>
<artifactId>gb28181-mocking-service</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>cn.skcks.docking.gb28181</groupId>
<artifactId>common</artifactId>
</dependency>
<dependency>
<groupId>cn.skcks.docking</groupId>
<artifactId>zlmediakit-service</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -24,8 +24,73 @@
<dependencies>
<dependency>
<groupId>cn.skcks.docking.gb28181</groupId>
<artifactId>starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>gb28181-mocking-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>cn.skcks.docking.gb28181</groupId>
<artifactId>gb28181-mocking-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>cn.skcks.docking.gb28181</groupId>
<artifactId>common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
@ -91,4 +156,4 @@
</testResource>
</testResources>
</build>
</project>
</project>

View File

@ -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);

19
pom.xml
View File

@ -17,6 +17,8 @@
<modules>
<module>gb28181-mocking-starter</module>
<module>gb28181-mocking-orm</module>
<module>gb28181-mocking-service</module>
<module>gb28181-mocking-api</module>
</modules>
<properties>
@ -54,6 +56,8 @@
<!-- <docker.registry.username>XXX</docker.registry.username>-->
<!-- <docker.registry.password>XXX</docker.registry.password>-->
<docker.maven.plugin.version>1.4.13</docker.maven.plugin.version>
<gb28181.docking.version>0.0.1-SNAPSHOT</gb28181.docking.version>
</properties>
<profiles>
@ -100,9 +104,22 @@
<dependency>
<groupId>cn.skcks.docking</groupId>
<artifactId>gb28181</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>${gb28181.docking.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>cn.skcks.docking.gb28181</groupId>
<artifactId>common</artifactId>
<version>${gb28181.docking.version}</version>
</dependency>
<dependency>
<groupId>cn.skcks.docking</groupId>
<artifactId>zlmediakit-service</artifactId>
<version>${gb28181.docking.version}</version>
</dependency>
<!--MapStruct-->
<dependency>
<groupId>org.mapstruct</groupId>