1、pom文件新增restdocs
org.springframework.restdocs spring-restdocs-mockmvctest
org.asciidoctor asciidoctor-maven-plugin1.5.8 generate-docs prepare-package process-asciidoc html book org.springframework.restdocs spring-restdocs-asciidoctor${spring-restdocs.version}
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、运行后会在targetgenerated-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']



