28

I have read following topic: Disabling Swagger with Spring MVC

and I wrote:

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

But in case if I try to access swagger ui: localhost:8080/swagger-ui.html
I see enter image description here

It looks not accurate. Can I fully disabled this URL ? 404 for example or something like this.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

10 Answers10

47

My answer is similar to the answer provided earlier with a slight difference. I usually create a separate spring profile named swagger. When I want to enable Swagger, l pass the following VM flag while starting my application, -Dspring.profiles.active=swagger. Here is an example of my Swagger configuration,

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

Next time when you try to access swagger-ui.html without swagger profile, you will get an empty Swagger screen but not 404.

enter image description here

If you don't want to load the static Swagger UI page at all, you can write a simple controller as shown below,

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

Now if you try to access swagger-ui.html without swagger profile, you will get a 404.

Indra Basak
  • 7,124
  • 1
  • 26
  • 45
19

with swagger 3.0.0 version you can add springfox.documentation.enabled=false in corresponding environment profile application.properties file. For example, I have added this to application-prod.properties to disable in production (while running the app you must specify the profile using VM args like -Dspring.profiles.active=prod)

asherbret
  • 5,439
  • 4
  • 38
  • 58
Vamsi Krishna DS
  • 615
  • 6
  • 17
  • 2
    This is the best answer anywhere for Swagger UI 3.0.0 and it was not easy to find (See my question here: https://stackoverflow.com/questions/67439123/swagger-3-0-0-cant-disable-in-production-without-swaggerconfig-and-profile). It is not even included in SpringFox's migration guide or current documentation. A very simple answer that should be added to SpringFox's documentation and migration guide post-haste. I would like to see a complete list of Spring Boot application properties in SpringFox's documentation also. – Geyser14 May 07 '21 at 17:57
  • 1
    This is the best one ! – Hikaru Shindo Jul 05 '21 at 16:09
  • When I try this, I get an infinite loop of `Unable to infer base url.` popup errors as the springfox.js script tries to execute the ui, security, & swagger-resources scripts, but can't handle the 404. – ScrappyDev Oct 26 '21 at 18:34
10

Adding onto @Hayden's answer (I don't have enough points to comment..)

According to the springdoc documentation, you can disable both the springdoc api endpoints and swagger-ui using the following properties:

https://springdoc.org/#disabling-the-springdoc-openapi-endpoints

# Disabling the /v3/api-docs endpoint
springdoc.api-docs.enabled=false

https://springdoc.org/#disabling-the-swagger-ui

# Disabling the swagger-ui
springdoc.swagger-ui.enabled=false
Henrik Solum
  • 101
  • 1
  • 5
8

You can externalize the @EnableSwagger2 to its own @Configruation and load it conditionally via a property or profile. e.g.

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

}

this would activate swagger for any profile that isn't production.

Darren Forsythe
  • 10,712
  • 4
  • 43
  • 54
4

If you dont have Swagger annotations inside controllers... just exclude SwaggerConfig.class and swagger dependencies on build

<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>
2

For SpringDoc users, add this to your application.properties

springdoc.api-docs.enabled=false

To disable Swagger only when the prod profile is active, add it to your application-prod.properties instead

Hayden
  • 61
  • 1
  • 3
1

In latest version of spring boot you can add this in yout application.yml :

springdoc:
  swagger-ui:
    enabled: false
  api-docs:
    enabled: false

So that swagger-ui key is used to disable the swagger interface and api-docs one is used to disable the route on which the JSON describing your API is served.

In my config I have a prod profile wich reads an application-prod.yml containing those lines.

Alexandre Liscia
  • 323
  • 5
  • 13
0

For those that use the code gen:

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

And add the file to you .swagger-codegen-ignore else your changes are overwritten on the next maven build

Jacques Koorts
  • 1,819
  • 1
  • 17
  • 10
0

When using springdoc-openapi-ui dependency one can disable swagger-ui through the property:

springdoc.swagger-ui.enabled=false

as stated in Spring Doc FAQ.

hugoalexandremf
  • 166
  • 1
  • 1
  • 9
-1

Just remove dependency.

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

It does not affect compiling.

Kymo Wang
  • 1,413
  • 2
  • 10
  • 9