1.导包
<!--引入swagger支持--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
2.写入配置类
需要两个配置类
DocumentationConfig
package cn.jiedada.hrm.config; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResourcesProvider; import java.util.ArrayList; import java.util.List; @Component @Primary public class DocumentationConfig implements SwaggerResourcesProvider { @Override public List<SwaggerResource> get() { List resources = new ArrayList<>(); //添加所有的微服务的文档资源 /** * 参数: * name:自定义的服务的名字 * location:指定是微服务的路径 在我们的配置文件中的zuul中的前缀和服务的别名 * /前缀/服务名/v2/api-docs */ resources.add(swaggerResource("系统管理", "/hrm/systemmanage/v2/api-docs", "2.0")); resources.add(swaggerResource("人力支援", "/hrm/hrmmm/v2/api-docs", "2.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; } }
SwaggerConfig
package cn.jiedada.hrm.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; 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; @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("http://localhost:1020") .contact(new Contact("jiedada", "", "jiedada@jiedada.cn")) .version("1.0") .build(); } }
3.访问方式
localhost:1020/swagger-ui.html