如何在spring-boot中完全禁用swagger-ui?(/ swagger-ui.html应返回404)

时间:2017-09-27 18:05:53

标签: java spring spring-boot swagger swagger-ui

我读过以下主题: Disabling Swagger with Spring MVC

我写道:

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.project.name.controller"))
            .paths(PathSelectors.ant("/api/**"))
            .build()
            .apiInfo(apiInfo())
            .enable(false);
}

但如果我试图访问swagger ui:localhost:8080/swagger-ui.html
我知道了 enter image description here

看起来不准确。我可以完全禁用此网址吗?例如404或类似的东西。

7 个答案:

答案 0 :(得分:26)

我的答案与之前提供的答案类似,略有不同。我通常会创建一个名为swagger的单独弹簧配置文件。当我想启用Swagger时,我在启动应用程序-Dspring.profiles.active=swagger时传递以下VM标志。这是我的Swagger配置的一个例子,

@Profile(value = {"swagger"})
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    ...
}

下次当您尝试在没有swagger-ui.html个人资料的情况下访问swagger时,您将获得一个空的Swagger屏幕,但不会显示404。

enter image description here

如果您根本不想加载静态Swagger UI页面,可以编写一个简单的控制器,如下所示,

@Profile("!swagger")
@RestController
@Slf4j
public class DisableSwaggerUiController {

    @RequestMapping(value = "swagger-ui.html", method = RequestMethod.GET)
    public void getSwagger(HttpServletResponse httpResponse) throws IOException {
        httpResponse.setStatus(HttpStatus.NOT_FOUND.value());
    }
}

现在,如果您尝试在没有swagger-ui.html个人资料的情况下访问swagger,则会获得404。

答案 1 :(得分:8)

使用 swagger 3.0.0 版本,您可以在相应的环境配置文件 Protected MustInherit Class Vehicle() Protected ReadOnly Property Serial_No As Integer Protected Property Mileage As Integer Protected Property Color As String Protected Function CreateVehicle() As Object End Function End Class Public Class Car Inherits Vehicle Public ReadOnly Property Car_Type As String Public Sub New() End Sub End Class 文件中添加 springfox.documentation.enabled=false。 例如,我已将此添加到 application.properties 以在生产中禁用(在运行应用程序时,您必须使用 VM args 指定配置文件,如 application-prod.properties

答案 2 :(得分:4)

您可以将@Configruation外部化为自己的@Profile("!production") @Configuration @EnableSwagger2 public class SwaggerConfiguration{ //Additional Swagger Beans ,并通过属性或个人资料有条件地加载它。 e.g。

function startInterval() {
    checkInt = setInterval(function() {
        $("#queueOrderList").load("updateQueueList.php function() {
            console.log("Load was performed");
        });
    }, 100)
}

}

这会激活任何非生产的配置文件的招摇。

答案 3 :(得分:1)

如果您在控制器内没有Swagger批注...只需排除SwaggerConfig.class和对构建的Swagger依赖项

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>com/company/app/SwaggerConfig.java</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>io.springfox</groupId>
                        <artifactId>springfox-swagger-ui</artifactId>
                    </exclude>
                    <exclude>
                        <groupId>io.springfox</groupId>
                        <artifactId>springfox-swagger2</artifactId>
                    </exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

答案 4 :(得分:0)

对于使用代码gen的用户:

@Controller
@Profile({"dev", "staging"})
public class HomeController {
    @RequestMapping(value = "/")
    public String index() {
        System.out.println("swagger-ui.html");
        return "redirect:swagger-ui.html";
    }
}

并向您添加文件。swagger-codegen-ignore,否则您的更改将在下一个Maven构建中被覆盖

答案 5 :(得分:0)

删除依赖即可。

<!--<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
</dependency>-->

不影响编译。

答案 6 :(得分:0)

对于 SpringDoc 用户,将此添加到您的 application.properties

springdoc.api-docs.enabled=false

要仅在 prod 配置文件处于活动状态时禁用 Swagger,请将其添加到您的 application-prod.properties