在 spring mvc 中禁用 swagger

Disable swagger in spring mvc

我是 Swagger 的新手并且已经使用 spring mvc 实现了 Swagger UI,我想在 production/live 环境中禁用 UI。我正在努力弄清楚。

这是我正在使用的

SwaggerConfig.java

@Configuration
@EnableSwagger2
public class SwaggerConfig {


}

RestController.java

    @RestController
    @Api(value="city", description="Operations pertaining to City Data")
    @RequestMapping(value="/v1/city")
    public class RestController {

    @ApiOperation(value = "View city by stateId or stateName")
        @RequestMapping(value="/search",method=RequestMethod.POST)
        public ResponseEntity<Object> getCityBystateId(@RequestBody StateDto stateDto,Model model){
        }
}

查看 Spring 的 profile 机制,让您在不同的环境中注册不同的 bean

当您根据文档 bootstrap 时,您可以在 class 级别上注释您的 swagger 配置 class,例如@Profile("dev"),从而为您选择的环境启用 swagger 配置

@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {


}

另一种方法是在您的生产环境中使用反向代理拒绝访问 Swagger Api。

在这种情况下,您的生产安装与您的 development/test 环境完全相同(然后更符合 DevOps 方法)并且您可以继续通过内部调用访问您的 Swagger API。