swagger2组件化

swagger2组件化,第1张

swagger2组件化 在pom中加入一下依赖
		
            org.springframework.boot
            spring-boot-starter-aop
            1.5.19.RELEASE
        

        
            io.springfox
            springfox-swagger2
            2.9.2
            
                
                    io.swagger
                    swagger-models
                
            
        
        
            io.swagger
            swagger-models
            1.5.21
        
        
            com.github.xiaoymin
            swagger-bootstrap-ui
            1.9.6
        
在resources目录下新建meta-INF/spring.factors

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
  com.demo.swagger.SwaggerAutoConfiguration
SwaggerProperties
package com.demo.swagger;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.linkedHashMap;
import java.util.List;
import java.util.Map;

@Component
public class SwaggerProperties {
    
    private Boolean enabled;
    
    private String title = "Swagger";
    
    private String description = "swagger";
    
    private String version = "1.0.1";
    
    private String license = "";
    
    private String licenseUrl = "";
    
    private String termsOfServiceUrl = "";

    private Contact contact = new Contact();

    
    private String basePackage = "";

    
    private List basePath = new ArrayList<>();
    
    private List excludePath = new ArrayList<>();

    
    private Map docket = new linkedHashMap<>();

    
    private String host = "";

    
    private List globalOperationParameters;

    public static class GlobalOperationParameter {
        
        private String name;

        
        private String description;

        
        private String modelRef;

        
        private String parameterType;

        
        private String required;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getModelRef() {
            return modelRef;
        }

        public void setModelRef(String modelRef) {
            this.modelRef = modelRef;
        }

        public String getParameterType() {
            return parameterType;
        }

        public void setParameterType(String parameterType) {
            this.parameterType = parameterType;
        }

        public String getRequired() {
            return required;
        }

        public void setRequired(String required) {
            this.required = required;
        }
    }

    public static class DocketInfo {
        
        private String title = "";
        
        private String description = "";
        
        private String version = "";
        
        private String license = "";
        
        private String licenseUrl = "";
        
        private String termsOfServiceUrl = "";

        private Contact contact = new Contact();

        
        private String basePackage = "";

        
        private List basePath = new ArrayList<>();
        
        private List excludePath = new ArrayList<>();

        private List globalOperationParameters;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getLicense() {
            return license;
        }

        public void setLicense(String license) {
            this.license = license;
        }

        public String getLicenseUrl() {
            return licenseUrl;
        }

        public void setLicenseUrl(String licenseUrl) {
            this.licenseUrl = licenseUrl;
        }

        public String getTermsOfServiceUrl() {
            return termsOfServiceUrl;
        }

        public void setTermsOfServiceUrl(String termsOfServiceUrl) {
            this.termsOfServiceUrl = termsOfServiceUrl;
        }

        public Contact getContact() {
            return contact;
        }

        public void setContact(Contact contact) {
            this.contact = contact;
        }

        public String getbasePackage() {
            return basePackage;
        }

        public void setbasePackage(String basePackage) {
            this.basePackage = basePackage;
        }

        public List getbasePath() {
            return basePath;
        }

        public void setbasePath(List basePath) {
            this.basePath = basePath;
        }

        public List getExcludePath() {
            return excludePath;
        }

        public void setExcludePath(List excludePath) {
            this.excludePath = excludePath;
        }

        public List getGlobalOperationParameters() {
            return globalOperationParameters;
        }

        public void setGlobalOperationParameters(List globalOperationParameters) {
            this.globalOperationParameters = globalOperationParameters;
        }
    }

    public static class Contact {
        
        private String name = "admin";
        
        private String url = "admin.com";
        
        private String email = "[email protected]";

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getLicense() {
        return license;
    }

    public void setLicense(String license) {
        this.license = license;
    }

    public String getLicenseUrl() {
        return licenseUrl;
    }

    public void setLicenseUrl(String licenseUrl) {
        this.licenseUrl = licenseUrl;
    }

    public String getTermsOfServiceUrl() {
        return termsOfServiceUrl;
    }

    public void setTermsOfServiceUrl(String termsOfServiceUrl) {
        this.termsOfServiceUrl = termsOfServiceUrl;
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public String getbasePackage() {
        return basePackage;
    }

    public void setbasePackage(String basePackage) {
        this.basePackage = basePackage;
    }

    public List getbasePath() {
        return basePath;
    }

    public void setbasePath(List basePath) {
        this.basePath = basePath;
    }

    public List getExcludePath() {
        return excludePath;
    }

    public void setExcludePath(List excludePath) {
        this.excludePath = excludePath;
    }

    public Map getDocket() {
        return docket;
    }

    public void setDocket(Map docket) {
        this.docket = docket;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public List getGlobalOperationParameters() {
        return globalOperationParameters;
    }

    public void setGlobalOperationParameters(List globalOperationParameters) {
        this.globalOperationParameters = globalOperationParameters;
    }
}


SwaggerAutoConfiguration
package com.demo.swagger;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.import;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.documentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.*;
import java.util.stream.Collectors;

@import( {
        Swagger2Configuration.class
})
public class SwaggerAutoConfiguration implements BeanFactoryAware {
    private static final String AUTH_KEY = "Authorization";

    private BeanFactory beanFactory;

    @Bean
    @ConditionalOnMissingBean
    public SwaggerProperties swaggerProperties() {
        return new SwaggerProperties();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "com.swagger.enabled", matchIfMissing = true)
    public List createRestApi(SwaggerProperties swaggerProperties) {
        ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) beanFactory;
        List docketList = new linkedList<>();

        if (swaggerProperties.getDocket().size() == 0) {
            final Docket docket = createDocket(swaggerProperties);
            configurableBeanFactory.registerSingleton("defaultDocket", docket);
            docketList.add(docket);
            return docketList;
        }

        for (String groupName : swaggerProperties.getDocket().keySet()) {
            SwaggerProperties.DocketInfo docketInfo = swaggerProperties.getDocket().get(groupName);

            ApiInfo apiInfo = new ApiInfoBuilder()
                    .title(docketInfo.getTitle().isEmpty() ? swaggerProperties.getTitle() : docketInfo.getTitle())
                    .description(docketInfo.getDescription().isEmpty() ? swaggerProperties.getDescription() : docketInfo.getDescription())
                    .version(docketInfo.getVersion().isEmpty() ? swaggerProperties.getVersion() : docketInfo.getVersion())
                    .license(docketInfo.getLicense().isEmpty() ? swaggerProperties.getLicense() : docketInfo.getLicense())
                    .licenseUrl(docketInfo.getLicenseUrl().isEmpty() ? swaggerProperties.getLicenseUrl() : docketInfo.getLicenseUrl())
                    .contact(new Contact(
                                    docketInfo.getContact().getName().isEmpty() ? swaggerProperties.getContact().getName() : docketInfo.getContact().getName(),
                                    docketInfo.getContact().getUrl().isEmpty() ? swaggerProperties.getContact().getUrl() : docketInfo.getContact().getUrl(),
                                    docketInfo.getContact().getEmail().isEmpty() ? swaggerProperties.getContact().getEmail() : docketInfo.getContact().getEmail()
                            ))
                    .termsOfServiceUrl(docketInfo.getTermsOfServiceUrl().isEmpty() ? swaggerProperties.getTermsOfServiceUrl() : docketInfo.getTermsOfServiceUrl())
                    .build();

            if (docketInfo.getbasePath().isEmpty()) {
                docketInfo.getbasePath().add("
    private List assemblyGlobalOperationParameters(
            List globalOperationParameters,
            List docketOperationParameters) {

        if (Objects.isNull(docketOperationParameters) || docketOperationParameters.isEmpty()) {
            return buildGlobalOperationParametersFromSwaggerProperties(globalOperationParameters);
        }

        Set docketNames = docketOperationParameters.stream()
                .map(SwaggerProperties.GlobalOperationParameter::getName)
                .collect(Collectors.toSet());

        List resultOperationParameters = Lists.newArrayList();
        if (Objects.nonNull(globalOperationParameters)) {
            for (SwaggerProperties.GlobalOperationParameter parameter : globalOperationParameters) {
                if (!docketNames.contains(parameter.getName())) {
                    resultOperationParameters.add(parameter);
                }
            }
        }

        resultOperationParameters.addAll(docketOperationParameters);
        return buildGlobalOperationParametersFromSwaggerProperties(resultOperationParameters);
    }
}

Swagger2Configuration
package com.demo.swagger;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.import;
import springfox.documentation.swagger2.configuration.Swagger2documentationConfiguration;

@Configuration
@ConditionalOnProperty(name = "com.demo.swagger.enabled", matchIfMissing = true)
@import({
        Swagger2documentationConfiguration.class
})
public class Swagger2Configuration {
}

在项目的pom文件中引入swagger,并在配置文件中配置com.demo.swagger.enabled,即可使用。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5717064.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存