1

我想自定义auto-section.adoc[]Spring Auto Rest Docs 生成的文件中的“标题”部分。@titleSpring Auto Rest Docs 使用方法上的 Javadoc 标记(如果存在)或方法名称(如docs中所述)解析部分标题,但我不能包含@title在方法的 Javadoc 标记中,因为控制器类来自其他 JAR,我也不想要默认生成的方法名称。那么,如何在 Spring Auto Rest Docs 中自定义章节标题。

例如在自动生成的 auto-section.adoc[]

我不想

=== Resolved Method Name

我想

=== Something else

有什么帮助吗?谢谢。

4

2 回答 2

0

Spring Auto REST Docs 通过查看@title标签来确定标题,如果找不到,则采用方法名称。目前无法直接自定义此行为。如果您无法修改 Javadoc,就像您的情况一样,您必须通过片段添加信息。至少有两种选择:

  1. 创建自定义模板。但是您仅限于片段可用的信息,因此硬编码文本没有很多替代方案。见https://scacap.github.io/spring-auto-restdocs/#snippets-customization
  2. 创建自定义片段。这使您可以完全控制所有内容,因此可以创建一个片段,将“其他东西”作为输入并将其用作标题。请参阅https://scacap.github.io/spring-auto-restdocs/#snippets-custom创建自定义片段和https://scacap.github.io/spring-auto-restdocs/#snippets-section-custom-snippet在部分片段中包含自定义片段。
于 2019-12-10T08:53:50.003 回答
0

我实现了如下自定义部分标题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 的帮助 :)

于 2019-12-11T09:47:25.007 回答