web-dev-qa-db-fra.com

Comment désactiver complètement swagger-ui dans spring-boot? (/ Swagger-ui.html doit renvoyer 404)

J'ai lu le sujet suivant: Désactivation de Swagger avec Spring MVC

et j'ai écrit:

@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);
}

Mais au cas où j'essayerais d'accéder à swagger ui: localhost:8080/swagger-ui.html
Je vois  enter image description here

Cela ne semble pas exact. Puis-je complètement désactiver cette URL? 404 par exemple ou quelque chose comme ça.

12
gstackoverflow

Ma réponse est similaire à la réponse fournie précédemment avec une légère différence. Je crée généralement un profil de ressort séparé nommé swagger. Lorsque je veux activer Swagger, je passe l'indicateur VM suivant lors du démarrage de mon application, -Dspring.profiles.active=swagger. Voici un exemple de ma configuration Swagger,

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

La prochaine fois que vous tenterez d'accéder au profil swagger-ui.html sans le profil swagger, vous obtiendrez un écran Swagger vide, mais pas le 404.

 enter image description here

Si vous ne souhaitez pas du tout charger la page statique de l'interface utilisateur Swagger, vous pouvez écrire un contrôleur simple, comme indiqué ci-dessous.

@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());
    }
}

Maintenant, si vous essayez d'accéder à swagger-ui.html sans le profil swagger, vous obtiendrez un 404.

22
Indra Basak

Vous pouvez externaliser le @EnableSwagger2 en son propre @Configruation et le charger de manière conditionnelle via une propriété ou un profil. par exemple.

@Profile("!production")
@Configuration
@EnableSwagger2
public class SwaggerConfiguration{
    //Additional Swagger Beans

}

cela activerait swagger pour tout profil qui n'est pas en production. 

3
Darren Forsythe

Si vous n'avez pas d'annotations Swagger dans les contrôleurs ... excluez simplement les dépendances SwaggerConfig.class et swagger lors de la construction.

<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>
0
Ramon Franquesa