springboot 关闭swagger (访问/swagger-ui.html报404)

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

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 enter image description here

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

3 Answers

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.

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.

shareedit

edited Sep 28 '17 at 17:33

answered Sep 27 '17 at 22:23

Indra Basak

3,4921620

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

add a comment

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>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值