引入以下依赖:html
<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.8.7</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>25.1-jre</version> </dependency>
在zuul模块中配置SwaggerResourcesProvidergit
@Component @Primary public class GatewaySwaggerResourcesProvider implements SwaggerResourcesProvider { private final RouteLocator routeLocator; public GatewaySwaggerResourcesProvider(RouteLocator routeLocator) { this.routeLocator = routeLocator; } @Override public List<SwaggerResource> get() { List<SwaggerResource> resources = new ArrayList<>(); List<Route> routes = routeLocator.getRoutes(); for (Route route:routes) { resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "v2/api-docs"))); } return resources; } private SwaggerResource swaggerResource(String name, String location) { SwaggerResource swaggerResource = new SwaggerResource(); swaggerResource.setName(name); swaggerResource.setLocation(location); swaggerResource.setSwaggerVersion("2.0"); return swaggerResource; } }
在zuul模块中配置SwaggerConfiggithub
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("说明文档") .description("接口说明文档") .termsOfServiceUrl("") .contact(new Contact("xhx","","")) .version("1.0") .build(); } }
在具体的服务模块中添加SwaggerConfigspring
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.dfove.dakx.auth.client.controller")) //basePage就是具体要扫描的包,有swagger注解的方法就生成到文档里面去 .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("用户系统api") .description("用户系统接口文档说明") .contact(new Contact("xhx", "", "")) .version("1.0") .build(); } @Bean UiConfiguration uiConfig() { return new UiConfiguration(null, "list", "alpha", "schema", UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L); } }
配置完成以后,在须要的接口上加上swagger的注解就能够将接口生成到文档里面了;bootstrap
输入http://localhost:8060/doc.html查看界面,这里选用swagger-bootstrap-ui,界面好看不少,端口号为网关端口号api