com.paas.swagger.content.PaaSServletBootStrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of paas-swagger-starter Show documentation
Show all versions of paas-swagger-starter Show documentation
Custom Swagger Spring Boot Starter
The newest version!
package com.paas.swagger.content;
import com.paas.aspect.RequestAspect;
import com.paas.swagger.bean.props.ApiInfoProps;
import com.paas.swagger.bean.props.OpenApiProps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.SmartLifecycle;
import org.springframework.util.CollectionUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @ClassName PaaSServletBootStrapper
* @Date 2020/7/23 11:06
* @Auther wangyongyong
* @Version 1.0
* @Description paas 平台容器
*/
public class PaaSServletBootStrapper implements SmartLifecycle
{
private static final Logger logger = LoggerFactory.getLogger(RequestAspect.class);
private final AtomicBoolean initialized = new AtomicBoolean(false);
@Autowired
private ConfigurableApplicationContext context;
@Autowired
private OpenApiProps openApiProps;
@Override
public boolean isAutoStartup()
{
return null != openApiProps;
}
@Override
public void stop(Runnable callback)
{
callback.run();
}
@Override
public void start()
{
if (initialized.compareAndSet(false, true))
{
logger.info("Swagger Context refreshed");
if (null != openApiProps && !CollectionUtils.isEmpty(openApiProps.getDocs()))
{
List apiInfos = openApiProps.getDocs();
for (ApiInfoProps apiInfo : apiInfos)
{
Contact contact = new Contact(apiInfo.getContactName(), "", apiInfo.getContactEmail());
ApiInfo api = new ApiInfoBuilder()
.title(apiInfo.getTitle())
.description(apiInfo.getDescription())
.version(apiInfo.getVersion())
.termsOfServiceUrl(apiInfo.getTermsOfServiceUrl())
.license(apiInfo.getLicense())
.licenseUrl(apiInfo.getLicenseUrl())
.contact(contact)
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName(apiInfo.getGroupName())
.select()
.apis(RequestHandlerSelectors.basePackage(apiInfo.getBasePackage()))
.build()
.apiInfo(api);
ConfigurableListableBeanFactory beanFactory = this.context.getBeanFactory();
beanFactory.registerSingleton(("openapi-" + apiInfo.getBasePackage()), docket);
}
}
}
}
@Override
public void stop()
{
initialized.getAndSet(false);
}
@Override
public boolean isRunning()
{
return initialized.get();
}
@Override
public int getPhase()
{
return 10001;
}
}