Java框架-Springboot框架-3(接口文档swagger2)

一、什么是swagger2

它是一个接口文档----用来前后端分离的一款文档。

二、swagger2的使用

1.引入swagger启动依赖

 <!--swagger文档-->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>
<!--图形化界面-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.7.8</version>
        </dependency>

2.创建swagger配置类

package com.gsh.MyConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

/**
 * @Auther: haohao
 * @Date:2022/7/2221:03
 */
@Configuration//配置文件注解

public class SwaggerConfig {
    @Bean
    //swagger中所有的功能都封装在Docket类中
    public Docket docket(){
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gsh.controller"))//为指定包下类生成接口文档
                .build();
        return docket;
    }
    //自己定义的文档信息
    private ApiInfo apiInfo(){
        //作者名,作者的联系地址,作者的邮箱
        Contact DEFAULT_CONTAC= new Contact("阿豪","http://www.baidu.com","你的邮箱");
        //文档,文档简介,文档版本,文档服务器地址,公司名,公司地址
        ApiInfo apiInfo=new ApiInfo("在线文档","最牛的文档","v1.0","http://jd.com",
                DEFAULT_CONTAC,"啊啊啊工作室","http://www.taobao.com",
                new ArrayList<VendorExtension>());
        return apiInfo;
    }
}

3. 开启swagger注解

@EnableSwagger2//开启swagger2的注解

4.使用swagger注解

@Api 接口类的注解---接口类上 tag属性

@ApiOperation 接口方法的注解---接口方法上 value:

@ApiImplicitParams( 接口参数的说明

{

                ApiImplicitParam() //单个参数的说明

  }

)

@ApiModel---- 实体类接口注解

@ApiModelProperty---->实体类属性的说明

5.访问swagger

第一种: http://localhost:8080/swagger-ui.html

第二种: http://localhost:8080/doc.html

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值