feat: aos-swagger模块开发完成
This commit is contained in:
@@ -28,9 +28,17 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aos</groupId>
|
||||
<artifactId>aos-nacos</artifactId>
|
||||
<artifactId>aos-swagger</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.aos.test;
|
||||
|
||||
import com.aos.swagger.enable.EnableSwagger;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableSwagger(applicationName = "test", scanPackagesPath = "com.aos.test")
|
||||
@EnableConfigurationProperties
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.aos.test.web;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author: zouyang
|
||||
* @date: 2022/6/1
|
||||
* @description: 需求计划入口
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/")
|
||||
@Api(value = "ActivityDemandController", tags = "需求计划入口")
|
||||
public class ActivityDemandController {
|
||||
|
||||
@PostMapping("/getListPage")
|
||||
@ApiOperation(value = "分页查询活动需求")
|
||||
public String getListPage() {
|
||||
return "1";
|
||||
}
|
||||
|
||||
@PostMapping("/getListPage1")
|
||||
@ApiOperation(value = "分页查询活动需求1")
|
||||
public String getListPage1() {
|
||||
return "2";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.aos.test.web2;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author: zouyang
|
||||
* @date: 2022/6/1
|
||||
* @description: 需求计划入口
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/test")
|
||||
@Api(value = "ActivityDemandController1", tags = "需求计划入口2")
|
||||
public class ActivityDemandController2 {
|
||||
|
||||
@PostMapping("/getListPage2")
|
||||
@ApiOperation(value = "分页查询活动需求2")
|
||||
public String getListPage() {
|
||||
return "1";
|
||||
}
|
||||
|
||||
@PostMapping("/getListPage12")
|
||||
@ApiOperation(value = "分页查询活动需求12")
|
||||
public String getListPage1() {
|
||||
return "2";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,3 +1,2 @@
|
||||
spring.cloud.nacos.config.server-addr=192.168.123.102:8848
|
||||
spring.application.name=aos-gateway
|
||||
|
||||
|
||||
@@ -35,5 +35,9 @@
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-spring-ui</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,9 +1,8 @@
|
||||
package com.aos.swagger.conf;
|
||||
|
||||
import com.aos.swagger.enable.EnableSwagger;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
@@ -13,8 +12,6 @@ import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -24,48 +21,59 @@ import java.util.Map;
|
||||
*/
|
||||
public class SwaggerConfig implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
|
||||
|
||||
//获取EnableEcho注解的所有属性的value
|
||||
/**
|
||||
* 获取注解参数
|
||||
*/
|
||||
Map<String, Object> attributes = annotationMetadata.getAnnotationAttributes(EnableSwagger.class.getName());
|
||||
//获取package属性的value
|
||||
List<String> applicationNames = Arrays.asList((String[]) attributes.get("applicationName"));
|
||||
List<String> scanPackagesPaths = Arrays.asList((String[]) attributes.get("scanPackagesPath"));
|
||||
int size = applicationNames.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
//使用beanDefinitionRegistry对象将EchoBeanPostProcessor注入至Spring容器中
|
||||
String applicationName = applicationNames.get(i);
|
||||
String scanPackagesPath = scanPackagesPaths.get(i);
|
||||
|
||||
beanDefinitionRegistry.registerBeanDefinition(applicationName, beanDefinitionBuilder.getBeanDefinition());
|
||||
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
|
||||
ConfigurableListableBeanFactory beanFactory = annotationConfigApplicationContext.getBeanFactory();
|
||||
beanFactory.registerSingleton();
|
||||
String applicationName = (String) attributes.get("applicationName");
|
||||
String scanPackagesPath = (String) attributes.get("scanPackagesPath");
|
||||
/**
|
||||
* 生成api信息
|
||||
*/
|
||||
Docket restApi = createRestApi(applicationName, scanPackagesPath);
|
||||
/**
|
||||
* 注册到bean容器
|
||||
*/
|
||||
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) beanDefinitionRegistry;
|
||||
beanFactory.registerSingleton(applicationName, restApi);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// @Value("${spring.application.name}")
|
||||
// private String applicationName;
|
||||
//
|
||||
public Docket createRestApi(String applicationName) {
|
||||
/**
|
||||
* 审查个好难过swagger对象
|
||||
*
|
||||
* @param applicationName
|
||||
* @param packagePath
|
||||
* @return springfox.documentation.spring.web.plugins.Docket
|
||||
* @Date 2022/11/6
|
||||
* @Author wangyl
|
||||
*/
|
||||
private Docket createRestApi(String applicationName, String packagePath) {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(apiInfo(applicationName))
|
||||
.groupName(applicationName)
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage("com.asura"))
|
||||
.apis(RequestHandlerSelectors.basePackage(packagePath))
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* api描述西信息
|
||||
*
|
||||
* @param applicationName
|
||||
* @return springfox.documentation.service.ApiInfo
|
||||
* @Date 2022/11/6
|
||||
* @Author wangyl
|
||||
*/
|
||||
private ApiInfo apiInfo(String applicationName) {
|
||||
return new ApiInfoBuilder()
|
||||
.title(applicationName + " swagger apis")
|
||||
.description("swagger-bootstrap-ui")
|
||||
.termsOfServiceUrl("")
|
||||
.title(applicationName + "api列表")
|
||||
.description("aos系统api")
|
||||
.version("1.0")
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,26 +2,29 @@ package com.aos.swagger.enable;
|
||||
|
||||
import com.aos.swagger.conf.SwaggerConfig;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @author: wangyl
|
||||
* @date: 2022/11/4
|
||||
* @description: 启动swagger的注解
|
||||
*/
|
||||
@Target({ElementType.TYPE})
|
||||
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Import({SwaggerConfig.class})
|
||||
@Import({SwaggerConfig.class, Swagger2DocumentationConfiguration.class})
|
||||
public @interface EnableSwagger {
|
||||
/**
|
||||
* swagger显示的名称
|
||||
*
|
||||
* @param
|
||||
* @return java.lang.String
|
||||
* @Date 2022/11/4
|
||||
* @Author wangyl
|
||||
*/
|
||||
String applicationName();
|
||||
|
||||
/**
|
||||
* 包扫描路径
|
||||
*/
|
||||
String scanPackagesPath();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user