【Java】Swagger2 结合spring boot 请求接口自动生成文档

时间:2021-06-04 05:44:18

官方地址:https://swagger.io/
功能主要有 1、提供后台访问地址,可对接口测试 2、生成各种离线文档 3、结合mock导入

swagger-ui

1、 maven依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
           

2、配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
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 api(){
        return  new Docket(DocumentationType.SWAGGER_2).select()
                // 接口包扫描,也可配置全项目扫描
                .apis(RequestHandlerSelectors.basePackage("com.xxxxx"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    // API自定义描述信息
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("xxx Project APIs")
                .description("xxxxxx 接口")
                .termsOfServiceUrl("http:xxxxxx")
                .version("1.0")
                .build();
    }

}

3、访问路径
可在此路径下进行接口查看,测试

http://ip:port/swagger-ui.html

离线文档生成

以生成Markdown格式文档 为例

前提swagger-ui 的访问路径是开启的,可访问 http://ip:port/v2/api-docs 返回的是接口描述的json

1、maven 依赖

<dependency>
            <groupId>io.github.swagger2markup</groupId>
            <artifactId>swagger2markup</artifactId>
            <version>1.3.1</version>
        </dependency>
<dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>

2、java 代码实现生成

import io.github.swagger2markup.GroupBy;
import io.github.swagger2markup.Language;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.github.swagger2markup.markup.builder.MarkupLanguage;
import org.junit.Test;

import java.net.URL;
import java.nio.file.Paths;

public class SwaggerCreateDocTest {


    @Test
    public void generateMarkdownDocs() throws Exception {
        //    输出Markdown格式
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                .withMarkupLanguage(MarkupLanguage.MARKDOWN)
                .withOutputLanguage(Language.ZH)
                .withPathsGroupedBy(GroupBy.TAGS)
                .withGeneratedExamples()
                .withoutInlineSchema()
                .build();

        Swagger2MarkupConverter.from(new URL("http://localhost:9999/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("C:\\Users\\wb-ndz525238\\Desktop"));
    }

}