swagger的配置访问及其使用

1.首先导入swagger的包,我这里使用的是gradle,在build.gradle中引入以下两个包
implementation ‘io.springfox:springfox-boot-starter:3.0.0’
implementation ‘com.github.xiaoymin:knife4j-spring-boot-starter:3.0.1’
使用maven的同学也可以按照maven导入包的方式进行导入包。

2.配置swagger的配置类,在你们项目中建立一个配置类,以下代码直接copy用,该配置Knife4j 是swagger的升级版 美化了swagger原生ui界面

package com.qunxiangyun.print.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {
  /**
     * apiInfo() 增加API相关信息
     * 所有的注解
     * .apis(RequestHandlerSelectors.any())
     * 指定部分注解1.Api.class(@APi),2.ApiOperation.class(@ApiOperation),3.ApiImplicitParam.class(@ApiImplicitParam)
     *.apis(RequestHandlerSelectors.withMethodAnnotation(Api.class))
     * 指定包路径
     * .apis(RequestHandlerSelectors.basePackage("这里填写需要的路径"))
     * .paths() 这个是包路径下的路径,PathSelectors.any()是包下所有路径
     */
    @Bean
    public Docket defaultApi() {
        //log.info(""+swaggerEnabled);
        return new Docket(DocumentationType.OAS_30)
                //.useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                //创建
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("XXX系统接口说明文档")
                .description("springboot | knife4j")
                // 作者信息
                //.contact("")
                .version("0.0.1")
                .build();
    }

    /*Api(tags = "xxx模块说明") 作用在模块类上 

    @ApiOperation("xxx接口说明") 作用在接口方法上 

    @ApiOperationSupport():(knife4j增加特性)用于接口方法排序,作者信息描述等。

    @ApiImplicitParam():对单个参数的说明
    
    @ApiParam("xxx参数说明") 作用在参数、方法和字段上,类似@ApiModelProperty 

    @ApiModel("xxxPOJO说明") 作用在模型类上:如VO、BO 

    @ApiModelProperty(value = "xxx属性说明",hidden = true) 作用在类方法和属性上,hidden设置为true可以隐藏该属性 */
}

这里,各位同学记得配置类加上去,这个很重要,不然出不来的,不要掉进坑里面了哦。

3.在yml文件中配置swagger的权限,这样是为了安全性,访问的时候输入账号和密码进行登录。

knife4j:
  enable: true
  basic:
    enable: true
    username: admin
    password: 123456

4.可以在controller中方法加上@ApiOperation(“”)注解,这样访问的时候看见的就是中文了,如

 @ApiOperation("添加工厂")
    public  ResultVo addFactory(@Validated @RequestBody FactoryInfoAddVO addVo){ 
  
   } 

这样登录swagger能在页面上看见中文了,就不用去找英文,也能快速的找到该功能的方法。

5.访问swagger
http://项目地址:端口号/doc.html
如:http://127.0.0.1:8080/doc.html
doc.html是swagger默认的
登进来之后输入账号和密码就行了,如配置里面的账号:admin,密码123456
在这里插入图片描述
随便点击一个方法,进入会看见文档,调试,open,swagger会自动填写该接口的请求参数、响应状态、响应状态、响应示例,是不是很方便呢,就再也不用给前端写接口文档了,哈哈哈;调试就类似于后端使用的测试工具postman;
好了,各位同学,到了这里就结束了,赶快动手使用swagger吧,我相信你会爱上的,对了,最后得提醒一下,访问swagger得把项目启动哦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值