♻️ 重构代码,支持部分Swagger参数化配置

This commit is contained in:
lishangbu committed 2018-07-26 17:58:38 +08:00
1 parent d79f000ce1
commit 0ad67ddef7
3 files changed
+99 -13

No files matched your search

@@ -62,7 +62,7 @@ public class SwaggerAutoConfiguration {
if (CollUtil.isEmpty(swaggerProperties.getBasePath())) {
swaggerProperties.getBasePath().add(BASE_PATH);
}
//noinspection unchecked
List<Predicate<String>> basePath = new ArrayList();
swaggerProperties.getBasePath().forEach(path -> basePath.add(PathSelectors.ant(path)));
@@ -70,13 +70,14 @@ public class SwaggerAutoConfiguration {
if (CollUtil.isEmpty(swaggerProperties.getExcludePath())) {
swaggerProperties.getExcludePath().add(DEFAULT_EXCLUDE_PATH);
}
List<Predicate<String>> excludePath = new ArrayList<>();
swaggerProperties.getExcludePath().forEach(path -> excludePath.add(PathSelectors.ant(path)));
//noinspection Guava
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.any())
.host(swaggerProperties.getHost())
.apiInfo(apiInfo(swaggerProperties)).select()
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
.paths(Predicates.and(Predicates.not(Predicates.or(excludePath)), Predicates.or(basePath)))
.build()
.securitySchemes(Collections.singletonList(securitySchema()))
@@ -86,7 +87,7 @@ public class SwaggerAutoConfiguration {
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth())
.forPaths(PathSelectors.ant("/**"))
.forPaths(PathSelectors.ant(BASE_PATH))
.build();
}
@@ -114,13 +115,15 @@ public class SwaggerAutoConfiguration {
return new OAuth("pigX OAuth", authorizationScopeList, grantTypes);
}
private ApiInfo apiInfo() {
private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
return new ApiInfoBuilder()
.title("PigX Swagger API ")
.description("https://gitee.com/log4j/pig/wikis")
.termsOfServiceUrl("https://gitee.wang/pig/pigx")
.contact(new Contact("冷冷", "https://gitee.wang/pig/pigx", "wangiegie@gmail.com"))
.version("2.0")
.title(swaggerProperties.getTitle())
.description(swaggerProperties.getDescription())
.license(swaggerProperties.getLicense())
.licenseUrl(swaggerProperties.getLicenseUrl())
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
.contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
.version(swaggerProperties.getVersion())
.build();
}
@@ -1,7 +1,25 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.swagger.config;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
@@ -9,12 +27,16 @@ import java.util.List;
/**
* SwaggerProperties
*
* @author: lisb
* @author: lengleng
* @date: 2018/7/25 14:00
*/
@Data
@ConfigurationProperties("swagger")
public class SwaggerProperties {
/**
* swagger会解析的包路径
**/
private String basePackage = "";
/**
* swagger会解析的url规则
**/
@@ -23,4 +45,56 @@ public class SwaggerProperties {
* 在basePath基础上需要排除的url规则
**/
private List<String> excludePath = new ArrayList<>();
/**
* 标题
**/
private String title = "PigX Swagger API";
/**
* 描述
**/
private String description = "https://gitee.com/log4j/pig/wikis";
/**
* 版本
**/
private String version = "2.0";
/**
* 许可证
**/
private String license = "Powered By PigX";
/**
* 许可证URL
**/
private String licenseUrl = "https://gitee.com/log4j/pig/wikis";
/**
* 服务条款URL
**/
private String termsOfServiceUrl = "https://gitee.wang/pig/pigx";
/**
* 忽略的参数类型
**/
private List<Class<?>> ignoredParameterTypes = new ArrayList<>();
/**
* host信息
**/
private String host = "";
private Contact contact = new Contact();
@Data
@NoArgsConstructor
public static class Contact {
/**
* 联系人
**/
private String name = "冷冷";
/**
* 联系人url
**/
private String url = "https://gitee.wang/pig/pigx";
/**
* 联系人email
**/
private String email = "wangiegie@gmail.com";
}
}
@@ -40,4 +40,13 @@ mybatis-plus:
cache-enabled: true
# swagger相关配置
swagger:
exclude-path: /actuator/**,/error
description: '全宇宙最牛逼的Spring Cloud微服务开发脚手架'
version: '1.2.4'
#base-package: com.pig4cloud.pigx.admin.controller
exclude-path:
- /actuator/**
- /error
contact:
name: '冷冷'
email: 'wangiegie@gmail.com'
url: 'https://gitee.wang/pig/pigx'