Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fully disable swagger-ui in spring-boot?(/swagger-ui.html should return 404)

Tags:

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.

like image 550
gstackoverflow Avatar asked Sep 27 '17 18:09

gstackoverflow


1 Answers

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.

like image 182
Indra Basak Avatar answered Sep 28 '22 07:09

Indra Basak