【发布时间】:2021-09-22 12:58:49
【问题描述】:
我浪费了很多时间试图弄清楚如何在 SpringFox 生成的 api-docs 的输出中为我的 Spring 控制器设置默认 MediaType (案卷 openapi v3.0)。最后,我发现了一个几乎未记录的接口 OperationBuilderPlugin,理论上它允许我在 OperationContext 中设置一些属性,但不幸的是,虽然我正在寻找的属性似乎在操作已建立:
@Override
public void apply(OperationContext context) {
context.operationBuilder()
.produces(new LinkedHashSet<>(
Collections.singletonList(MediaType.APPLICATION_JSON_VALUE)));
}
还尝试将 produces 直接设置到 Docket 中,但仍然没有成功
@Bean
public Docket api() {
....
return new Docket(DocumentationType.OAS_30)
.securityContexts(Collections.singletonList(securityContext()))
.securitySchemes(Collections.singletonList(authenticationScheme))
.useDefaultResponseMessages(true)
.consumes(new HashSet<>(Arrays.asList(MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE)))
.produces(new LinkedHashSet<>(Arrays.asList("application/json", "application/xml")))
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
...
}
我不想在控制器类级别(或方法级别)指定返回 MediaType,所以如果我找不到更清洁的解决方案,我最后的机会是在我之后手动将 */* 替换为我想要的媒体类型从远程 url 下载 api-docs.json。
有人遇到过同样的问题吗? 任何帮助将不胜感激,在此先感谢
【问题讨论】:
标签: spring-boot openapi springfox-boot-starter