
com.zlyx.easyapi.config.ApiConfigurer Maven / Gradle / Ivy
package com.zlyx.easyapi.config;
import java.text.MessageFormat;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Developer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.zlyx.easycore.list.Lists;
import com.zlyx.easycore.tool.Ops;
import io.swagger.config.SwaggerConfig;
import io.swagger.models.Contact;
import io.swagger.models.Info;
import io.swagger.models.Swagger;
@Component
public class ApiConfigurer implements SwaggerConfig {
private ServletContext servletContext;
private ApiProperties apiProperties;
@Autowired
public ApiConfigurer(ServletContext servletContext, @Autowired(required = false) ApiProperties apiProperties) {
if(apiProperties == null) {
apiProperties = new ApiProperties();
}
this.apiProperties = apiProperties;
this.servletContext = servletContext;
}
private static String mavenDependency = "<dependency>
"
+ " <groupId>{0}</groupId>
"
+ " <artifactId>{1}</artifactId>
"
+ " <version>{2}</version>
" + "</dependency>
";
@Override
public Swagger configure(Swagger swagger) {
Info info = swagger.getInfo();
if (info == null) {
info = new Info();
swagger.setInfo(info);
}
info.setTitle(apiProperties.getName());
info.setDescription(MessageFormat.format(mavenDependency, apiProperties.getGroupId(),
apiProperties.getArtifactId(), apiProperties.getVersion()));
info.setVersion(apiProperties.getVersion());
info.setTermsOfService(apiProperties.getUrl());
List developers = Lists.newArrayList();
List emails = Lists.newArrayList();
if (!apiProperties.getDevelopers().isEmpty()) {
List developerList = apiProperties.getDevelopers();
for (Developer developer : developerList) {
developers.add(developer.getName());
emails.add(developer.getEmail());
}
}
Contact contact = new Contact().name(Ops.toString(developers, ", ")).email(Ops.toString(emails, ", "))
.url(apiProperties.getUrl());
info.setContact(contact);
setBashPath(swagger);
return swagger;
}
private void setBashPath(Swagger swagger) {
if (StringUtils.isEmpty(swagger.getBasePath())) {
swagger.setBasePath(
StringUtils.isEmpty(servletContext.getContextPath()) ? "/" : servletContext.getContextPath());
}
}
@Override
public String getFilterClass() {
return null;
}
public ApiProperties getApiProperties() {
return apiProperties;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy