【问题标题】:Asciidoctor "Snippets" is missing in MavenMaven 中缺少 Asciidoctor “Snippets”
【发布时间】:2020-09-19 17:00:10
【问题描述】:

当我运行测试用例时,adoc 类型的请求和响应将被创建并以 JSON 格式显示在 generated-sn-ps 目录下。当我运行这个 mvn 命令 mvn clean package 在生成的文档下创建 jar 和 HTML 类型的索引时,出现以下警告

当我通过浏览器打开 index.html 时,会显示 Unresolved directive in index.adoc 而不是 JSON 结果作为响应。

pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

     
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.oracle/ojdbc6 -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-asciidoctor</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>

         <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <finalName>config-demo</finalName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>1.5.6</version>
                <executions>
                    <execution>
                        <id>generate-docs</id>
                        <phase>package</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>html</backend>
                            <doctype>product</doctype>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

index.adoc

= Nafaz Benzema Getting Started With Spring REST Docs

    This is an example output for a service running at http://localhost:9090:
    
==GET API Example

.request
include::{snippets}/getAllProduct/http-request.adoc[]

.response
include::{snippets}/getAllProduct/http-response.adoc[]

As you can see the format is very simple, and in fact you always get the same message.

控制器测试类

@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
@WebMvcTest
@AutoConfigureRestDocs(outputDir = "/target/generated-snippets")
public class ProductControllerTest {


    @Autowired
    private WebApplicationContext webApplicationContext;


    @MockBean
    private ProductService productService;

    private MockMvc mockMvc;

    List<Product> products =null;

    @BeforeEach
    public void setUp(WebApplicationContext webApplicationContext, RestDocumentationContextProvider documentationContextProvider) {
        this.mockMvc = MockMvcBuilders
                .webAppContextSetup(webApplicationContext)
                .apply(MockMvcRestDocumentation.documentationConfiguration(documentationContextProvider))
                .build();

        products=getProducts();
    }

    @Test
    public void getAllProduct() throws Exception {

        String expectedProduct=new ObjectMapper().writeValueAsString(products);

        Mockito.when(productService.getAllProduct()).thenReturn(products);

        MvcResult result= mockMvc.perform(get("/products")
                .contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(status().isOk())
                .andExpect(content().json(expectedProduct))
                .andDo(document("{methodName}",preprocessRequest(prettyPrint()),
                        preprocessResponse(prettyPrint())))
                .andReturn();

        String actualProduct=result.getResponse().getContentAsString();

        Assertions.assertEquals(expectedProduct,actualProduct);

    }

    private List<Product> getProducts()
    {
        Product product_1=new Product();
        product_1.setProductId(1001);
        product_1.setProductName("Penguin-ears");
        product_1.setNumberOfUnitInCartoon(20);
        product_1.setPriceOfCartoon(175.00);
        product_1.setUrlOfImage("https://i.ibb.co/pLdM7FL/shutterstock-306427430-scaled.jpg");

        Product product_2=new Product();
        product_2.setProductId(1002);
        product_2.setProductName("Horseshoe");
        product_2.setNumberOfUnitInCartoon(5);
        product_2.setPriceOfCartoon(825);
        product_2.setUrlOfImage("https://i.ibb.co/MRDwnqj/horseshoe.jpg");

        return new ArrayList<>(Arrays.asList(product_1,product_2));
    }
}

【问题讨论】:

    标签: spring-boot junit5 asciidoc asciidoctor spring-restdocs


    【解决方案1】:

    已找到解决方案。我们必须在它所属的插件中添加spring-restdocs-asciidoctor 依赖。

    从全局依赖管理空间中移除spring-restdocs-asciidoctor依赖并添加到插件中

     <plugin>
                    <groupId>org.asciidoctor</groupId>
                    <artifactId>asciidoctor-maven-plugin</artifactId>
                    <version>1.5.6</version>
                    <executions>
                        <execution>
                            <id>generate-docs</id>
                            <phase>package</phase>
                            <goals>
                                <goal>process-asciidoc</goal>
                            </goals>
                            <configuration>
                                <backend>html</backend>
                                <doctype>product</doctype>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.springframework.restdocs</groupId>
                            <artifactId>spring-restdocs-asciidoctor</artifactId>
                            <version>2.0.5.RELEASE</version>
                        </dependency>
                    </dependencies>
                </plugin>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 2011-05-09
      • 2014-01-04
      • 2013-07-12
      • 1970-01-01
      • 2014-06-28
      • 2015-05-08
      相关资源
      最近更新 更多