引入依赖
<!--swagger --><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.3</version></dependency><!--解决Swagger 2.9.2版本NumberFormatException--><dependency><groupId>io.swagger</groupId><artifactId>swagger-models</artifactId><version>1.6.0</version></dependency><dependency><groupId>io.swagger</groupId><artifactId>swagger-annotations</artifactId><version>1.6.0</version></dependency>
配置文件
swagger:production: falsebasic:enable: trueusername: xxxxpassword: xxxx
配置类
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Config {@Value("${swagger.basic.enable:false}")Boolean swaggerEnable;@Beanpublic Docket buildDocket() {return new Docket(DocumentationType.SWAGGER_2).enable(swaggerEnable).host("127.0.0.1:8080").apiInfo(buildApiInf()).select().apis(RequestHandlerSelectors.basePackage("com.gemplate")).paths(PathSelectors.any()).build();}private ApiInfo buildApiInf() {return new ApiInfoBuilder().title("模板项目").description("模板项目描述").version("1.0.0").build();}
}
--------------------------------------------------分割线---------------------------------------------------
----------------------------------------非网关项目不需要了解----------------------------------------
网关统一swagger配置
需要单项目已支持swagger
网关项目单独配置类
@Configuration
@Primary //代表是Swagger主 配置
@AllArgsConstructor
public class SwaggerResourcesConfig implements SwaggerResourcesProvider {private final RouteLocator routeLocator;private final GatewayProperties gatewayProperties;@Overridepublic List<SwaggerResource> get() {List<SwaggerResource> resources = new ArrayList<>();List<String> routes = new ArrayList<>();routeLocator.getRoutes().subscribe(route -> {if (!ApiDocsUtil.isContains(route.getId())) {routes.add(route.getId());}});gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())).forEach(route -> {route.getPredicates().stream().filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName())).forEach(predicateDefinition -> resources.add(swaggerResource(route.getId(),predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0").replace("**", ApiDocsUtil.API_URI))));});return resources;}private SwaggerResource swaggerResource(String name, String location) {SwaggerResource swaggerResource = new SwaggerResource();swaggerResource.setName(name);swaggerResource.setLocation(location);swaggerResource.setSwaggerVersion("1.0");return swaggerResource;}
}
提供网关swagger获取子项目swagger接口
/*** 获取api接口信息*/
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerController {@Autowired(required = false)private SecurityConfiguration securityConfiguration;@Autowired(required = false)private UiConfiguration uiConfiguration;private final SwaggerResourcesProvider swaggerResources;@Autowiredpublic SwaggerController(SwaggerResourcesProvider swaggerResources) {this.swaggerResources = swaggerResources;}@GetMapping("/configuration/security")public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {return Mono.just(new ResponseEntity<>(Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));}@GetMapping("/configuration/ui")public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {return Mono.just(new ResponseEntity<>(Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));}@GetMapping("")public Mono<ResponseEntity<?>> swaggerResources() {return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));}
}