SpringMVC如何在生产环境禁用Swagger的方法

2025-05-29 0 93

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。

Swagger 让部署管理和使用功能强大的API从未如此简单。好吧,以上是官方的说法,我直接复制的,在我看来swagger就是一个接口文档管理器,以前我们写接口一般都是world编写,但是有一个问题就是测试的时候需要依赖第三方工具,GET的接口还好,直接浏览器打开,POST的只能依赖另外的工具了,而Swagger呢,可以直接通过代码中的注解生成接口文档(JavaEE),一般人都用这种方式,而且直接集成在项目中,方便成员查看,同时还能直接测试,另外Swagger的界面也不错,也许这就是我选择用Swagger的原因吧,直接官方说的RESTful 风格那个不用管,不是RESTful 风格的接口也能用,当然Swagger还有一种方式就是手动写接口说明了,这样的好处就是代码只有代码,因为一旦代码中添加了Swagger的接口注解后,代码量还是增加了不少,当然坏处就是你改完了代码,还要去改接口文档

SpringMVC集成springfox-swagger2和springfox-swagger-ui很简单,只需要两步:

(1)pom中添加依赖

?

1

2

3

4

5

6

7

8

9

10
<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>${springfox-swagger.version}</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>${springfox-swagger.version}</version>

</dependency>

(2)添加Swagger的配置类:

?

1

2

3

4

5

6
@Configuration

@EnableSwagger2

@EnableWebMvc

@ComponentScan("com.XXX.controller")

public class SwaggerConfig{

}

然后就可以通过http://localhost/swagger-ui.html看到项目中所有的接口信息了,通过http://localhost/v2/api-docs就能看到json数据。

但是,如何在生产环境禁用这些api文档呢?试了很多种方式,最终找到一个简单实用的办法:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45
@Configuration

@EnableSwagger2

@EnableWebMvc

@ComponentScan("com.XXX.controller")

public class SwaggerConfig{

@Autowired

ConfigService configService;

@Bean

public Docket customDocket() {

if(configService.getServerEnv() == ServerEnvEnum.ONLINE) {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfoOnline())

.select()

.paths(PathSelectors.none())//如果是线上环境,添加路径过滤,设置为全部都不符合

.build();

}else {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo());

}

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title("XXX系统")

.description("XXX系统接口")

.license("")

.licenseUrl("")

.termsOfServiceUrl("")

.version("1.0.0")

.contact(new Contact("","", ""))

.build();

}

private ApiInfo apiInfoOnline() {

return new ApiInfoBuilder()

.title("")

.description("")

.license("")

.licenseUrl("")

.termsOfServiceUrl("")

.version("")

.contact(new Contact("","", ""))

.build();

}

}

现在http://localhost/swagger-ui.html这个页面虽然还能访问,那是却看不到任何内容了,包括http://localhost/v2/api-docs也是一样。

应该还有更好的办法!

参考:https://www.zzvips.com/article/153552.html

swagger必须要跟springmvc在同一个context才行,springmvc只是spring的一个子context。如果swagger让spring的context加载,那么swagger的那些url用springmvc的拦截器是拦截不到的!

所以,两种解决办法:

如果是使用注解的方式:

(1)spring-mvc的配置:

?

1

2

3

4

5
<!-- 使用Annotation自动注册Bean,只扫描@Controller -->

<context:component-scan base-package="com.inspur.eyun.yunbx" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

<context:include-filter type="assignable" expression="com.inspur.eyun.yunbx.swagger.SwaggerConfig"/>

</context:component-scan>

注意要把swagger的配置加进来,同时:

(2)spring的配置:

?

1

2

3

4

5
<!-- 包扫描、注解相关 -->

<context:component-scan base-package="com.inspur.eyun.yunbx">

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

<context:exclude-filter type="assignable" expression="com.inspur.eyun.yunbx.swagger.SwaggerConfig"/>

</context:component-scan>

注意把swagger排除掉

(3)Swagger的配置:

?

1

2

3

4

5

6
@Configuration

@EnableSwagger2

@EnableWebMvc

@ComponentScan("com.inspur.eyun.yunbx.controller")

public class SwaggerConfig{

}

注意@Configuration注解。

当然更推荐的办法是使用xml配置的方式,因为这样可以不用引入swagger的依赖包:

(1)spring-mvc的配置:

?

1

2

3

4

5
<!-- 使用Annotation自动注册Bean,只扫描@Controller -->

<context:component-scan base-package="com.inspur.eyun.yunbx" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

<import resource="classpath:spring-mvc-swagger.xml" />

spring-mvc-swagger.xml:

?

1

2

3

4

5

6

7

8

9

10
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

<description>SpringMVC Swagger Configuration</description>

<!-- swagger配置,生产环境置空 -->

<bean class="com.inspur.eyun.yunbx.swagger.SwaggerConfig" />

</beans>

注意:我们这里把swagger单独放到一个配置文件中,如果是线上环境,则文件内容为空,如果是线下测试环境,则配置上Swagger

(2)spring的配置:

?

1

2

3

4
<!-- 包扫描、注解相关 -->

<context:component-scan base-package="com.inspur.eyun.yunbx">

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

(3)Swagger的配置:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
@EnableSwagger2

@EnableWebMvc

public class SwaggerConfig{

@Bean

public Docket customDocket() {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.select()

.apis(RequestHandlerSelectors.basePackage("com.inspur.eyun.yunbx.controller"))

.paths(PathSelectors.any())

.build();

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title("XXX平台")

.description("XXX平台接口")

.license("")

.licenseUrl("")

.termsOfServiceUrl("")

.version("1.0.0")

.contact(new Contact("","", ""))

.build();

}

}

注意:这里我们去掉了@Configuration,同时,修改我们的pom,配置多profile打包:

pom.xml:

?

1

2

3

4

5

6

7

8

9

10

11

12

13
<!-- Swagger -->

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>${springfox-swagger.version}</version>

<scope>${swagger.scope}</scope>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<scope>${swagger.scope}</scope>

<version>${springfox-swagger-ui.version}</version>

</dependency>

注意:这里的依赖的scope是动态设置的,如果是线上环境,我们把scope设置成provided就可以。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
<profiles>

<profile>

<id>dev</id>

<properties>

<profiles.active>dev</profiles.active>

<swagger.scope>compile</swagger.scope>

</properties>

<activation>

<activeByDefault>true</activeByDefault>

</activation>

</profile>

<profile>

<id>test</id>

<properties>

<profiles.active>test</profiles.active>

<swagger.scope>compile</swagger.scope>

</properties>

</profile>

<profile>

<id>online</id>

<properties>

<profiles.active>online</profiles.active>

<swagger.scope>provided</swagger.scope>

</properties>

</profile>

</profiles>

通过不同的profile给swagger的依赖设置不同的scope!

注意:springfox-swagger.version=2.7.0有bug,可以使用低版本2.6.1。太他妈的坑!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/goldenfish1919/article/details/78280051

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringMVC如何在生产环境禁用Swagger的方法 https://www.kuaiidc.com/112493.html

相关文章

发表评论
暂无评论