标签:项目版本 vararg resource otv sources source 教程 path imp
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
swagger:
enable: true
application-name: xxx系统API说明
application-version: 1.0
application-description: springfox swagger 3.0整合Demo
try-host: http://localhost:${server.port}
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties("swagger") @Data @AllArgsConstructor @NoArgsConstructor public class SwaggerProperties { /** * 是否开启swagger,生产环境一般关闭,所以这里定义一个变量 */ private Boolean enable; /** * 项目应用名 */ private String applicationName; /** * 项目版本信息 */ private String applicationVersion; /** * 项目描述信息 */ private String applicationDescription; /** * 接口调试地址 */ private String tryHost; }
@Configuration public class SwaggerConfig implements WebMvcConfigurer { private final SwaggerProperties swaggerProperties; public SwaggerConfig(SwaggerProperties swaggerProperties) { this.swaggerProperties = swaggerProperties; } @Bean public Docket createRestApi() { return new Docket( DocumentationType.OAS_30) .pathMapping("/") // 定义是否开启swagger,false为关闭,可以通过变量控制 .enable(swaggerProperties.getEnable()) // 将api的元信息设置为包含在json ResourceListing响应中。 .apiInfo(apiInfo()) // 接口调试地址 .host(swaggerProperties.getTryHost()) // 选择哪些接口作为swagger的doc发布 .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build() // 支持的通讯协议集合 .protocols(newHashSet("https", "http")); } /** * API 页面上半部分展示信息 */ private ApiInfo apiInfo() { return new ApiInfoBuilder().title(swaggerProperties.getApplicationName() + " Api Doc") .description(swaggerProperties.getApplicationDescription()) .contact(new Contact("lighter", null, "123456@gmail.com")) .version("Application Version: " + swaggerProperties.getApplicationVersion() + ", Spring Boot Version: " + SpringBootVersion.getVersion()) .build(); } @SafeVarargs private final <T> Set<T> newHashSet(T... ts) { if (ts.length > 0) { return new LinkedHashSet<>(Arrays.asList(ts)); } return null; } }
http://localhost:8080/swagger-ui/
springfox-swagger-ui-3.0.0.jar 下 META-INF.resources.webjars.springfox-swagger-ui
6、更多操作参考CSDN
Swagger 3.0配置整合使用教程_南伯基尼-CSDN博客
标签:项目版本 vararg resource otv sources source 教程 path imp
原文地址:https://www.cnblogs.com/harriets-zhang/p/14499870.html