wagger的展示路径修改

本文介绍如何在SpringBoot项目中集成Swagger2,并提供两种配置方案。一种是简单的配置方式,另一种是可以自定义展示链接的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转:https://www.jianshu.com/p/ce7e247515f5?utm_source=oschina-app

 

:本文是基于springboot配置实现,但在实际中使用springmvc和本文的配置基本一致,不影响使用。

下面是第一种配置方式。尽量精简的配置。

1 在pom文件中引入依赖

<!-- Swagger -->
<dependency>
    <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> 

2 新建swag配置文件
在代码中新定义一个类,代码如下

@Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("对外开放接口API 文档") .description("HTTP对外开放接口") .version("1.0.0") .termsOfServiceUrl("http://xxx.xxx.com") .license("LICENSE") .licenseUrl("http://xxx.xxx.com") .build(); } } 

3 在自己的controller中使用

@Slf4j
@RestController
public class MyController { @RequestMapping(value = "hello", method = RequestMethod.GET) public String hello() { return "hello"; } } 

如上的配置就可以了,下面是展示效果
在本地启动项目,然后浏览器输入:

http://localhost:8080/swagger-ui.html
效果如下:

 
swagger.png

 

这种配置方式非常简单,对原有的代码也基本没有任何侵入性。基本可以满足接口描述的需求。

但是,有很多项目都是前后端分离的,在nginx中会配置把 "/rest/" 开头的链接打到后端来,而把其他请求打到前端去。当然,你可以修改nginx的配置,把某些链接打到前端去,剩下的直接打到后端。不过这种方式会有一定的安全性问题,可控性也不太好。最好能给修改swagger的展示地址,
比如从
http://localhost:8080/swagger-ui.html
修改为
http://localhost:8080/rest/api/doc/swagger-ui.html

下面就是第二种配置方式,可以自定义展示链接。

1 在pom文件中引入依赖(注意我们去掉了对springfox-swagger-ui的依赖

<!-- Swagger -->
<dependency>
    <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> 

2 git clone swagger-ui项目

https://github.com/swagger-api/swagger-ui
请选择2.0以上,3.0以下版本

将其中的dist文件夹拷贝到自己项目中的resources/swagger目录下,如图


 
dist.png

3 在resources下新建swagger.properties文件,其中的内容为

springfox.documentation.swagger.v2.path=/rest/api/doc

4 修改resources/swagger/dist 下的index文件
将其中的

  <script type="text/javascript"> $(function () { var url = window.location.search.match(/url=([^&]+)/); if (url && url.length > 1) { url = decodeURIComponent(url[1]); } else { url = "http://petstore.swagger.io/v2/swagger.json"; } 

url = "http://petstore.swagger.io/v2/swagger.json"
修改为
url = "/rest/api/doc"
修改后如下

  <script type="text/javascript"> $(function () { var url = window.location.search.match(/url=([^&]+)/); if (url && url.length > 1) { url = decodeURIComponent(url[1]); } else { url = "/rest/api/doc"; } 

5 新建swag配置文件
在代码中新定义一个类,代码如下

@Configuration
@EnableSwagger2
@PropertySource("classpath:swagger.properties") // 新增对swagger.properties 的引入 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("对外开放接口API 文档") .description("HTTP对外开放接口") .version("1.0.0") .termsOfServiceUrl("http://xxx.xxx.com") .license("LICENSE") .licenseUrl("http://xxx.xxx.com") .build(); } } 

6 增加url映射
如果是springboot,在Application中增加

@SpringBootApplication
public class DemoApplication extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/rest/api/doc/**").addResourceLocations("classpath:/swagger/dist/"); } ..... } 

如果是springmvc,则在api-servlet.xml(application.xml)中增加

<mvc:resources mapping="/rest/api/doc/**" location="classpath:/swagger/dist/"/> 

终于配置完成,启动项目,在浏览器中输入:

http://localhost:8080/rest/api/doc/index.html

 
swagger.png

 

springfox官网

http://springfox.github.io/springfox/

api注解相关描述可参考

http://blog.youkuaiyun.com/xupeng874395012/article/details/68946676



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值