Web Services Sping Boot -仅在Swagger UI中公开端点,而不通过直接HTTP调用

gywdnpxw  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(124)

有没有可能只在Swagger UI中公开一些选定的端点,但通过直接HTTP调用使它们在服务器上不可用?
@Operation(hidden=true)不暴露Swagger用户界面中的端点,但仍然在服务器上可用。我只需要“反转行为”。
用例:我们通常禁止在生产中使用Swagger UI。我希望在开发期间有一些仅在Swagger UI中可用的端点用于测试目的。
版本:Sping Boot 2.6.2,springdoc-openapi-ui 1.6.3。

jvlzgdj9

jvlzgdj91#

使用案例:

  • 我们通常禁止在生产中使用Swagger UI。
  • 我希望在开发过程中有一些仅在Swagger UI中可用的端点,用于测试目的。

这种(需求类型)的“最灵活”的解决方案可能是配置文件!
我们可以:

@SpringBootApplication(exclude = {
  SpringDocWebMvcConfiguration.class,
  SpringDocConfiguration.class
})
public class MySpringApp {...

从我们的主配置中排除openapi配置(默认配置文件)(因为它是被禁止的)。
然后我们就介绍:

@Configuration
@Profile("documented") // ! this gets only activated/loaded, when "documented" is (one of) spring.aprofiles.active
@Import({
  SpringDocWebMvcConfiguration.class,
  SpringDocConfiguration.class
})
// customize API here ...
class DocConfig {
  // ...and/or here
}
  • 所有 * 我们想要“昂首阔步”的控制器,我们也注解:
@Profile("documented") 
@[Rest]Controller public class MyDevController {
...

不幸的是,我们只能在bean方法/类上使用@Profile,要按“请求Map”(方法)使用它,我们必须复制并隔离控制器:
其中一个:

@Profile("documented") // work in progress

和原始控制器,所述原始控制器具有:

@Profile("!documented") // as in prod

我们必须相互排除它们("documented""!documented"),因为否则(路径)Map将不明显。
这样,在生产环境中运行我们的应用程序(没有“文档化”配置文件)将:

  • 跳过springdoc配置
  • 曝光号:
  • 昂首阔步
  • 无API端点
  • 不加载任何配置文件为“文档化”的控制器。

在dev/loaclly中运行我们的应用,我们将设置spring.profiles.active=documented,springdoc将:

  • 公开UI和端点:
  • “记录的”(& default!)控制器。

相关问题