Swagger的基本用法

swagger是解决前后端对接问题的API框架,用它可以自动生成接口文档

  • 准备工作
    使用swagger之前需要导入两个依赖
    • Springfox-swagger2
    • swagger-springmvc
    对应的依赖写法:
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.2</version>
</dependency>

导入依赖后需要编写一个配置类

@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {
     
}

当我们什么都不配置时,启动项目,打开网址
访问测试 :http://localhost:8080/swagger-ui.html
会看到Swagger的默认界面

swagger默认页面
我们可以在配置类中自定义配置页面的各中信息,比如如下代码

  1. Swagger的实例Bean是Docket,所以可以通过配置Docket配置swagger
 //配置docket以配置Swagger具体参数
@Bean
public Docket docket() {
   
   return new Docket(DocumentationType.SWAGGER_2);
}
  1. 要配置页面基本信息还需要配置一个apiInfo()属性
private ApiInfo apiInfo() {
   
   Contact contact = new Contact("联系人名字", "http://xxx.xxx.com/联系人访问链接", "联系人邮箱");
   return new ApiInfo(
           "Swagger学习", // 标题
           "学习演示如何配置Swagger", // 描述
           "v1.0", // 版本
           "http://terms.service.url/链接", // 组织链接
           contact, // 联系人信息
           "Apach 2.0 许可", // 许可
           "许可链接", // 许可连接
           new ArrayList<>()
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值