java - Spring Rest Doc 不生成 html

标签 java spring api-doc spring-restdocs

我关注了 getting started guide逐字获取 Spring Rest Doc,但我无法从生成的片段中获取任何 html。

在我配置的目录 (build/generated-snippets) 中可以很好地生成片段,但我看不到任何 html5/目录,其中包含从片段生成的 html 文件。

文档 at some point说明如何将文档打包到 jar 中,很明显它需要 html5/目录中的一些文件,但这不是在构建运行时创建的:

dependsOn asciidoctor
from("${asciidoctor.outputDir}/html5") {
    into 'static/docs'
}

我错过了什么?

我的项目文件,build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE'
    }
}

plugins {
    id 'org.asciidoctor.convert' version '1.5.2'
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'war'

sourceCompatibility = 1.8
targetCompatibility = 1.8

ext { 
    snippetsDir = file('build/generated-snippets')
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web:1.3.5.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-logging:1.3.5.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.3.5.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-rest:1.3.5.RELEASE'
    compile 'org.springframework.cloud:spring-cloud-starter-aws:1.1.0.RELEASE'
    compile 'org.postgresql:postgresql:9.4.1208'
    compile 'commons-io:commons-io:2.5'

    testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc:1.1.0.RELEASE'   
    testCompile 'org.springframework.restdocs:spring-restdocs-core:1.1.0.RELEASE'
    testCompile 'org.springframework.boot:spring-boot-starter-test:1.3.5.RELEASE'
}

jacoco {
    toolVersion = "0.7.6.201602180812"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

test {
    outputs.dir snippetsDir
    jacoco {
        append = false
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        classDumpFile = file("$buildDir/jacoco/classpathdumps")
    }
}

asciidoctor { 
    attributes 'snippets': snippetsDir 
    inputs.dir snippetsDir 
    dependsOn test 
}

war {
    dependsOn asciidoctor
    from("${asciidoctor.outputDir}/html5") {
        into 'static/docs'
    }

    baseName = project_name
    version = version
    manifest {
        attributes(
            'Implementation-Title': project_name,
            'Implementation-Version': version
        )
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.13'
}

还有一个我用来测试的简单测试文件:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class ApiDocumentation
{
    @Rule
    public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("build/generated-snippets");

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setUp()
    {
        mockMvc = MockMvcBuilders.webAppContextSetup(context)
                .apply(documentationConfiguration(restDocumentation))
                .build();
    }

    @Test
    public void testIndex() throws Exception
    {
        mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andDo(document("index"));
    }

}

最佳答案

src/main/asciidoc (Maven) 或 src/docs/asciidoc (Gradle) 下创建一个 .adoc 文件(如 api-guide.adoc)并引用到生成的片段。然后在指定目录下生成html。

关于java - Spring Rest Doc 不生成 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38057946/

相关文章:

Spring Boot Data Rest POST 返回 204 但只有 SELECTS

spring - 如何使用远程 token 服务?

swagger-ui - OpenAPI 生成器 : wrong format for the example value of date datatype. 是否可以自定义?

java - 如何处理groovy中的数字

java - 绘制一个java类的依赖关系图

java - 存储/检索椭圆曲线加密 (ECC) 公钥和私钥

java - 线程 "main"java.util.NoSuchElementException 与 Scanner.nextInt() 中出现异常

java - 即使使用 jackson-datatype-jsr310,Instant 也无法序列化为适当的格式

swagger - Swagger 2.0 将如何处理参数或返回类型中的泛型?

spring-boot - 在 OpenApi 3 中展平 RequestParam 对象