Swagger学习

目录

Swagger是什么?

Swagger的作用

Spring boot 继承 swagger

        1.建立一个spring boot项目,添加web依赖。

        2.添加swagger依赖。       

           3.新建一个controller类

        4.添加一个swagger配置类

        5.启动项目,访问http://localhost:8080/swagger-ui.html#/

Swagger配置

        1.因为swagger实例的bean是Docket,所以我们通过配置一个Docket来配置swagger。

        2.配置apiInfo()属性来配置文档信息

        1.配置如下所示

       2. 在docket后加上apiInfo

        3.重启项目,刷新swagger

        3.配置扫描接口

                通过select来配置扫描什么接口

          4.配置接口扫描过滤

                通过paths进行扫描过滤

        5.通过enable来控制swagger的开关

        6.配置api分组

 实体配置

           1.新建一个实体类

        2.添加一个请求路径

        3.重启项目进行查看

常用注解

        @Api(tags = "xxx模块说明")

        @ApiOperation("xxx接口说明")

        @ApiModel("xxxPOJO说明")

        @ApiModelProperty(value = "xxx属性说明",hidden = true)

        @ApiParam("xxx参数说明")


Swagger是什么?

        Swapper号称是世界上最流行的api的框架。

        官网:API Documentation & Design Tools for Teams | Swagger

Swagger的作用

        编写代码的同时更新api文档,解决前后端的交流问题。

Spring boot 继承 swagger

        1.建立一个spring boot项目,添加web依赖。

        2.添加swagger依赖。       

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
         <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

           3.新建一个controller类

package cn.tedu.swagger;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

        4.添加一个swagger配置类

package cn.tedu.swagger;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration //配置类交给spring管理
@EnableSwagger2    //开启swagger
public class SwaggerConfig {
}

        5.启动项目,访问http://localhost:8080/swagger-ui.html#/

成功后如图所示:

Swagger配置

        1.因为swagger实例的bean是Docket,所以我们通过配置一个Docket来配置swagger。

@Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2);
    }

        2.配置apiInfo()属性来配置文档信息

        1.配置如下所示

 private ApiInfo apiInfo() {
        Contact contact = new Contact("联系人名字", "http://xxx.xxx.com/联系人访问链接", "联系人邮箱");
        return new ApiInfo(
                "Swagger学习", // 标题
                "配置Swagger", // 描述
                "v7.77", // 版本
                "http://localhost:8080/swagger-ui.html#/组织链接", 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值