整合swagger接口文档统一地址
设置生产环境是否显示
设置账号密码
pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
application.properties
# name
spring.application.name=forty-zuul
# swagger 扫描路径 同zuul.routes.*.path
swagger.doc.api.name=cms,oa
swagger.production=false
swagger.basic.enable=true
swagger.basic.username=admin
swagger.basic.password=tsino@2019
# swagger 是否显示
swagger.enable=true
# zuul
zuul.routes.forty-cms.path=/cms/**
zuul.routes.forty-cms.service-id=forty-cms
zuul.routes.forty-oa.path=/oa/**
zuul.routes.forty-oa.service-id=forty-oa
Swagger2Config.java // 其他项目上也需要有此配置文件,注意扫描包路径
package com.forty.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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 springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @Description swagger配置
* @author forty
* @date 2019年9月2日
*
*/
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Config {
@Value("${spring.application.name}")
private String applicationName;
@Value("${swagger.enable}")
private boolean swaggerEnable;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(swaggerEnable)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.forty.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("forty( " + applicationName + " )接口文档")
.description("forty( " + applicationName + " )接口文档")
.termsOfServiceUrl("http://localhost:8080")
.contact(new Contact("forty", "https://www.forty.com", "forty@163.com"))
.version("1.0")
.build();
}
}
SwaggerDocConfig.java
//只在入口项目增加此类
// 也有使用DiscoveryClient方式的,不过我这边显示是double的所以写成配置文件形式
package com.forty.config;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
@Component
@Primary
public class SwaggerDocConfig implements SwaggerResourcesProvider {
@Value("${swagger.doc.api.name}")
private String[] apiNames;
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
for (String apiName : apiNames) {
resources.add(swaggerResource(apiName, "/" + apiName + "/v2/api-docs", "1.0"));
}
return resources;
}
private SwaggerResource swaggerResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}