1. 程式人生 > >swagger-ui 介面工具 spingboot 整合 swagger

swagger-ui 介面工具 spingboot 整合 swagger

swagger ui 是一個介面工具 提供介面測試 介面文件等功能

 

 

整合步驟

https://github.com/swagger-api/swagger-ui 下載 swagger 然後把 swagger中的dest 放到 專案中resource 中

2 引入maven配置檔案

       <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger2</artifactId>

            <version>2.2.2</version>

        </dependency>

        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger-ui</artifactId>

            <version>2.2.2</version>

        </dependency>

3 編寫配置類 

package com.perovider.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StopWatch;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static springfox.documentation.builders.PathSelectors.regex;
import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * Created by Administrator on 2018/8/5.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
    public static final String DEFULT_INCLUDE_PATTERN = "/user/.*";// 你的介面url
    private final Logger log = LoggerFactory.getLogger(SwaggerConfig.class);
    
    @Bean
    public Docket swaggerSpringfoxDocket() {
        log.debug("開始啟動swagger api 工具 ");
        StopWatch watch = new StopWatch();
        watch.start();
        ApiInfo apiInfo = new ApiInfo("使用者管理測試文件", "介面的描述", "介面版本1",
                "termOfServiceUrl", "contact", "license", "licenseUrl");

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .genericModelSubstitutes(ResponseEntity.class)
                .forCodeGeneration(true)
                .directModelSubstitute(java.time.LocalDate.class, String.class)
                .directModelSubstitute(java.time.ZonedDateTime.class, Date.class)
                .directModelSubstitute(java.time.LocalDateTime.class, Date.class)
                .select()
                .paths(regex(DEFULT_INCLUDE_PATTERN)) //介面url
                .build();
        
        watch.stop();
        log.debug("啟動swagger api 工具用時 {} ms",watch.getTotalTimeMillis());
        return docket;
    }
}

 4 輸入專案地址+swagger-ui.html  http://localhost:9001/swagger-ui.html

效果如下