我实现了如下自定义部分标题auto-section.adoc
:
1)我创建了扩展的自定义部分片段SectionSnippet
:
class CustomSectionSnippet extends SectionSnippet {
private final String title;
public CustomSectionSnippet(final Collection<String> sectionNames, final boolean skipEmpty,
final String title) {
super(sectionNames, skipEmpty);
this.title = title;
}
@Override
protected Map<String, Object> createModel(final Operation operation) {
final Map<String, Object> model = super.createModel(operation);
if (title != null) {
model.put("title", title);
}
return model;
}
}
2)然后是扩展的自定义部分构建器SectionBuilder
:
class CustomSectionBuilder extends SectionBuilder {
private Collection<String> snippetNames = DEFAULT_SNIPPETS;
private final boolean skipEmpty = false;
private String title;
@Override
public CustomSectionBuilder snippetNames(final String... snippetNames) {
this.snippetNames = Arrays.asList(snippetNames);
return this;
}
public CustomSectionBuilder sectionTitle(final String title) {
this.title = title;
return this;
}
@Override
public SectionSnippet build() {
return new CustomSectionSnippet(snippetNames, skipEmpty, title);
}
}
3)然后像这样使用它:
@Test
void testApi() throws Exception {
final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("name", "test");
this.mockMvc.perform(post("/api")
.params(params)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(commonDocumentation(
new CustomSectionBuilder()
.sectionTitle("Something else") // <-- custom section title
.snippetNames(
AUTO_AUTHORIZATION,
AUTO_REQUEST_FIELDS,
REQUEST_HEADERS,
REQUEST_PARAMETERS,
RESPONSE_FIELDS,
CURL_REQUEST,
HTTP_RESPONSE)
.build()
));
}
现在我可以将Something else
部分标题作为部分标题传递,该部分标题将包含在自动生成的auto-section.adoc
文件中。
感谢@florian-benz 的帮助 :)