快速上手 Swagger

Swagger2

入门案例

        <!-- 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>

config


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

@Configuration
@EnableSwagger2//开启swagger
public class SwaggerConfig {

}

controller

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello,swagger";
    }
}

访问:http://localhost:8080/swagger-ui.html

添加配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

import java.util.ArrayList;


@Configuration
@EnableSwagger2//开启swagger
public class SwaggerConfig {

    @Bean//配置swagger的Docket bean实例
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    //配置swagger信息  apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact DEFAULT_CONTACT = new Contact("JL", "https://www.baidu.com/", "111111@qq.com");
        return new ApiInfo(
                " JL Swagger",//标题
                "冲冲冲",//描述
                "L1.0",//版本号
                "https://www.baidu.com/",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}

在这里插入图片描述

解释swagger页面

在这里插入图片描述
在这里插入图片描述

配置多个分组

这里可以看出groupName是给分组名称的
在这里插入图片描述

如何添加多个分组?

配置多个docket对象

@Configuration
@EnableSwagger2//开启swagger
public class SwaggerConfig {

    @Bean//配置swagger的Docket bean实例
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("默认")
                //enable表示 是否启动swagger false:swagger不能在浏览器访问
//                .enable(false)
                .select()
                /*RequestHandlerSelectors:配置要扫描接口方式
                  basePackage:扫描指定包下面的接口
                   any:扫描全部
                   none:不扫描
                   withClassAnnotation:扫描类上的注解
                   withMethodAnnotation:扫描方法上的注解
                   */
                .apis(RequestHandlerSelectors.basePackage("com.jiang.controller"))
                //过滤
//                .paths(PathSelectors.ant("/com/jiang/**"))
                .build();
    }
    
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("1");
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("2");
    }
    
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("3");
    }

    //配置swagger信息  apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact DEFAULT_CONTACT = new Contact("JL", "https://www.baidu.com/", "111111@qq.com");
        return new ApiInfo(
                " JL Swagger",//标题
                "冲冲冲",//描述
                "L1.0",//版本号
                "https://www.baidu.com/",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}

在这里插入图片描述

注释

@ApiModel(“用户实体类”)//文档注释

在这里插入图片描述

@ApiOperation(value=“上传文件”, notes=“测试FastDFS文件上传”)

在这里插入图片描述

@ApiParam(“文件”)

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值