com.nepxion.discovery.console.configuration.SwaggerConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of discovery-console Show documentation
Show all versions of discovery-console Show documentation
Nepxion Discovery is an enhancement for Spring Cloud Discovery
package com.nepxion.discovery.console.configuration;
/**
* Title: Nepxion Discovery
* Description: Nepxion Discovery
* Copyright: Copyright (c) 2017-2050
* Company: Nepxion
* @author Haojun Ren
* @version 1.0
*/
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
@Configuration
@EnableSwagger2
@ConditionalOnProperty(value = "swagger.service.enabled", matchIfMissing = true)
public class SwaggerConfiguration {
public static final String BASE_PACKAGE = "com.nepxion.discovery.console.endpoint";
@Value("${spring.application.name}")
private String serviceName;
@Value("${swagger.service.description:Console Restful APIs}")
private String description;
@Value("${swagger.service.version:1.0.0}")
private String version;
@Value("${swagger.service.license.name:Apache License 2.0}")
private String license;
@Value("${swagger.service.license.url:http://www.apache.org/licenses/LICENSE-2.0}")
private String licenseUrl;
@Value("${swagger.service.contact.name:Haojun Ren}")
private String contactName;
@Value("${swagger.service.contact.url:https://github.com/Nepxion/Discovery}")
private String contactUrl;
@Value("${swagger.service.contact.email:[email protected]}")
private String contactEmail;
@Value("${swagger.service.termsOfServiceUrl:http://www.nepxion.com}")
private String termsOfServiceUrl;
@Autowired(required = false)
private List swaggerHeaderParameters;
@Bean("discoveryDocket")
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("discovery")
.apiInfo(apiInfo())
.select()
.apis(SwaggerConfiguration.basePackage(BASE_PACKAGE)) // 扫描该包下的所有需要在Swagger中展示的API,@ApiIgnore注解标注的除外
.paths(PathSelectors.any())
.build()
.globalOperationParameters(swaggerHeaderParameters);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(serviceName)
.description(description)
.version(version)
.license(license)
.licenseUrl(licenseUrl)
.contact(new Contact(contactName, contactUrl, contactEmail))
.termsOfServiceUrl(termsOfServiceUrl)
.build();
}
public static Predicate basePackage(String basePackage) {
return new Predicate() {
@Override
public boolean apply(RequestHandler input) {
return declaringClass(input).transform(handlerPackage(basePackage)).or(true);
}
};
}
private static Function, Boolean> handlerPackage(String basePackage) {
return new Function, Boolean>() {
@Override
public Boolean apply(Class> input) {
String[] subPackages = basePackage.split(DiscoveryConstant.SEPARATE);
for (String subPackage : subPackages) {
boolean matched = input.getPackage().getName().startsWith(subPackage.trim());
if (matched) {
return true;
}
}
return false;
}
};
}
@SuppressWarnings("deprecation")
private static Optional extends Class>> declaringClass(RequestHandler input) {
return Optional.fromNullable(input.declaringClass());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy