为了账号安全,请及时绑定邮箱和手机立即绑定

springboot集成restdocs输出接口文档

标签:
Java

1、pom文件新增restdocs

<dependency>                  
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>    
<scope>test</scope>
</dependency>
<plugin>    
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>    
<version>1.5.8</version>    
    <executions>        
        <execution>            
            <id>generate-docs</id>            
            <phase>prepare-package</phase>            
            <goals>                
                <goal>process-asciidoc</goal>            
            </goals>            
            <configuration>                
                <backend>html</backend>                
                <doctype>book</doctype>            
            </configuration>        
        </execution>    
    </executions>    
<dependencies>        
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-asciidoctor</artifactId>
        <version>${spring-restdocs.version}</version>
    </dependency>    
</dependencies>
</plugin>

2、在test包下新建controller

@RunWith(SpringRunner.class)
@SpringBootTestpublic class ApiBaseController {    

    @Rule    
    public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();    
    
    public MockMvc mockMvc;    
    
    @Autowired    
    private WebApplicationContext context;    
    
    @Before    
    public void setUp() {        
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
                    .apply(documentationConfiguration(this.restDocumentation))
                    .build();   
    }
}

3、新建单元测试

public class TestController extends ApiBaseController {
    @Test    
    public void test() throws Exception  {        
        super.mockMvc.perform(post("/test").param("aaa", "bbb")                
                        .contentType(MediaType.APPLICATION_JSON))                
                        .andDo(document("{ClassName}/{methodName}", 
                        preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()),                        
                        requestParameters(parameterWithName("aaa").description("查询名字"))));    
     }
 }

4、运行后会在target\generated-snippets生成adoc文件
5、在main下新建asciidoc包,新建index.adoc 拼接已经生成的adoc

= API:toc: 
left:toclevels: 4
[[order-it]]
== 1. 订单接口
[[order-query]]
=== 订单查询
operation::TestController/test[snippets='curl-request,http-request']
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消