使用swagger2markup生成pdf接口文档

集成swagger相关依赖:略

然后pom.xml中添加如下依赖:


    io.github.swagger2markup
    swagger2markup
    1.3.1

    
        
            false
        
        jcenter-releases
        jcenter
        http://jcenter.bintray.com
    

添加asciidoctor相关插件:


    
        ...
        ...

        
            org.asciidoctor
            asciidoctor-maven-plugin
            1.5.6
            
                
                    org.asciidoctor
                    asciidoctorj-pdf
                    1.5.0-alpha.15
                
            
            
                
                    output-html
                    generate-resources
                    
                        process-asciidoc
                    
                
             
             
                src/docs/asciidoc/generated
                src/docs/asciidoc/pdf
                pdf
             
        
    

添加测试用例:

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 org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class ApiApplicationTests {
    @Test
    public void generateAsciiDocs() throws Exception {
        //    输出Ascii格式
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                .withMarkupLanguage(MarkupLanguage.ASCIIDOC)
                .build();

        Swagger2MarkupConverter.from(new URL("http://localhost:8080/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFile(Paths.get("src/docs/asciidoc/generated/all"));

    }
}

分别执行:

mvn test
mvn asciidoctor:process-asciidoc

最终生成目录结构:

使用swagger2markup生成pdf接口文档_第1张图片

 

导出后的pdf会出现中文丢失的情况,参考这篇:

swagger+asciidoctor 导出PDF中文缺失乱码问题解决

 

关联连接:

使用Swagger2Markup实现API文档的静态部署(一):AsciiDoc

Introduction to Asciidoctor in Java

 

你可能感兴趣的:(杂项)