springboot 关闭swagger (访问/swagger-ui.html报404)
How to fully disable swagger-ui in spring-boot?(/swagger-ui.html should return 404)
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
asked Sep 27 '17 at 18:05
9,82245169355
3 Answers
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.
answered Sep 27 '17 at 22:23
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
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.
answered Sep 27 '17 at 18:56
3,663823
-
1
I tried this. behaviour the same – gstackoverflow Sep 27 '17 at 21:44
0
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>