https://stackoverflow.com/questions/46454473/how-to-fully-disable-swagger-ui-in-spring-boot-swagger-ui-html-should-return-4
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.
java spring spring-boot swagger swagger-ui
shareedit
edited Sep 28 '17 at 7:47
asked Sep 27 '17 at 18:05
gstackoverflow
9,82245169355
add a comment
activeoldestvotes
20
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.
shareedit
edited Sep 28 '17 at 17:33
answered Sep 27 '17 at 22:23
Indra Basak
3,4921620
Did you try to access swagger-ui? What did you see? – gstackoverflow Sep 28 '17 at 8:06
I get an empty swagger screen but not 404. Updated my posting. – Indra Basak Sep 28 '17 at 17:02
Yes, I got approximately the same. Actually browser asks me to type base url. But I want to get 404 – gstackoverflow Sep 28 '17 at 17:30
Updated my posting. I got a 404 when I tried with the new controller. – Indra Basak Sep 28 '17 at 17:34
Not work to me, why? – Gank May 10 '18 at 1:18
How are you starting your service? – Indra Basak May 10 '18 at 3:58
add a comment
3
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.
shareedit
answered Sep 27 '17 at 18:56
Darren Forsythe
3,663823
1
I tried this. behaviour the same – gstackoverflow Sep 27 '17 at 21:44
add a comment
0
If you dont have Swagger annotations inside controllers... just exclude SwaggerConfig.class and swagger dependencies on build
org.apache.maven.plugins
maven-compiler-plugin
com/company/app/SwaggerConfig.java
org.springframework.boot
spring-boot-maven-plugin
io.springfox
springfox-swagger-ui
io.springfox
springfox-swagger2
repackage