Step1.下载: https://github.com/swagger-api/swagger-ui.git
Step2.找到dist目录,将整个dist目录复制到需要使用Swagger-UI工具项目的resources目录下
Step3. pom.xml中引入依赖:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency>
Step4.在项目中创建一个Swagger-UI的配置类SwaggerConfiguration:
@Configuration @EnableSwagger2 public class SwaggerConfiguration { //定义API接口映射路径 public static final String DEFAULT_INCLUDE_PATTERN = "/order/.*"; private final Logger log = LoggerFactory.getLogger(SwaggerConfiguration.class); @Bean public Docket swaggerSpringfoxDocket() { log.debug("Starting Swagger"); StopWatch watch = new StopWatch(); watch.start(); //用于生成对应API接口文档的描述信息,可省略 ApiInfo apiInfo = new ApiInfo("订单管理API接口测试文档","description", "termsOfServiceUrl","contact","version","",""); Docket docket = new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo) .genericModelSubstitutes(ResponseEntity.class) .forCodeGeneration(true) .genericModelSubstitutes(ResponseEntity.class) .directModelSubstitute(java.time.LocalDate.class, String.class) .directModelSubstitute(java.time.ZonedDateTime.class, Date.class) .directModelSubstitute(java.time.LocalDateTime.class, Date.class) .select() .paths(regex(DEFAULT_INCLUDE_PATTERN))//匹配路径生成对应接口文档 .build(); watch.stop(); log.debug("Started Swagger in {} ms", watch.getTotalTimeMillis()); return docket; } }
Step5. 测试: http://服务地址IP (localhost) +微服务端口+/swagger-ui.html