Swagger的详细使用教程

目录

 

一.Swagger的作用

二.Swagger的详细使用步骤

一.Swagger的作用

swagger用于生成在线api文档和进行接口测试,是前后端联调中使用最多的工具

二.Swagger的详细使用步骤

1.引入Swagger依赖

      <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <scope>provided </scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <scope>provided </scope>
        </dependency>

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>provided </scope>
       </dependency>

2.创建swagger配置类 

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2) //类型:swagger
                .groupName("webApi") //分组名称
                .apiInfo(webApiInfo()) //设置在线文档的信息
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }

    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("java", "http://atguigu.com", "55317332@qq.com"))
                .build();
    }
}

3.若创建的swagger是新建的一个模块(若是在当前模块引入swager依赖,此步可以忽略),则:

(1)将swagger模块的坐标导入依赖到要使用swagger的模块中 

(2)在启动类上添加@ComponentScan(basePackages={"swagger配置类所在的包路径"})

          假设:swagger所在包:com.atguigu.servicebase

                    启动类所在包:com.atguigu.service

                   则配置为@ComponentScan(basePackages = {"com.atguigu"})

4.启动项目,访问http://ip:接口/swagger-ui.html进入swagger的控制台

若有弹窗问题,则要刷新maven,重启项目

5.接口测试

页面中已经显示了可以测试的接口,点击要测试的接口,value处输入参数,点击Try it out发送

6.swagger生成api文档的常用帮助注解:

以下这些注解用于生成api文档时,接口的提示信息

@Api(description = "讲师模块"):用在类上
@ApiOperation(value = "讲师列表"):用在方法上
@ApiParam(name = "id",value = "讲师id",required = true):用在参数上

@ApiModel:用在类上,标记类是swagger的解析类

            属性:  Value:为模型提供备用名称   description:提供详细的类描述

@ApiModelProperty:用在@A皮Model标记的类的属性上,

           属性:   Value: 属性简要说明

@ApiModel

效果如下:

​ 

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Swagger是一种API文档自动生成工具,可以根据API接口代码自动生成对应的API文档。使用Swagger可以大大减少API文档编写工作量,并且能够让开发人员更加关注API接口的实现。 要使用Swagger,需要先在项目中添加Swagger依赖。例如,对于使用Spring Boot框架的项目,可以添加如下依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> ``` 在代码中,需要添加Swagger的配置信息,主要包括API文档的基本信息、API接口的扫描范围等信息。例如: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.api")) .paths(PathSelectors.any()) .build(); } } ``` 这个配置文件指定了API接口定义所在的包路径,并且指定了所有请求路径都生成对应的API文档。 在代码中添加这些配置后,项目启动后,通过访问http://localhost:8080/swagger-ui.html即可查看生成的API文档,可以查看API接口的详细说明、输入输出参数等信息,并且可以直接在页面上进行测试调用API接口。 总之,Swagger可以极大地提高接口管理的效率,降低了接口文档的工作难度。加上其强大的可扩展性,Swagger可以建立更好的API文档体系,成为API的重要建设工具。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十八岁讨厌Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值