Reputation: 37034
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
It looks not accurate. Can I fully disabled this URL ? 404 for example or something like this.
Upvotes: 28
Views: 45862
Reputation: 121
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
Upvotes: 12
Reputation: 328
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.
Upvotes: 1
Reputation: 316
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.
Upvotes: 0
Reputation: 101
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
Upvotes: 2
Reputation: 1513
Just remove dependency.
<!--<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>-->
It does not affect compiling.
Upvotes: -1
Reputation: 733
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
)
Upvotes: 21
Reputation: 1827
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
Upvotes: 0
Reputation: 41
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>
Upvotes: 4
Reputation: 7394
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.
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.
Upvotes: 48
Reputation: 11411
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.
Upvotes: 8