快速上手 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
    评论
Swagger是一种用于设计、构建、文档化和使用RESTful Web服务的开源工具集。它提供了一种简单且强大的方式来描述API,以及生成交互式文档、客户端SDK和服务端存根代码。下面是关于Swagger的入门到精通的步骤: 1. 安装Swagger:首先,你需要安装Swagger工具集。你可以从Swagger官方网站或者通过包管理工具(如npm、pip等)来安装Swagger。 2. 创建Swagger文档:一旦安装完成,你可以开始创建Swagger文档。Swagger使用YAML或JSON格式来描述API。你可以通过编辑器(如Swagger Editor)或者直接编写YAML/JSON文件来创建文档。 3. 定义API:在Swagger文档中,你需要定义API的各个方面,包括路径、HTTP方法、请求参数、响应数据等。你可以使用Swagger提供的规范来定义API的各个部分。 4. 添加元数据:除了API定义,你还可以添加一些元数据,如API的标题、描述、版本号等。这些信息将在生成的文档中显示,并帮助用户更好地理解和使用你的API。 5. 生成文档:一旦你完成了Swagger文档的编写,你可以使用Swagger工具集中的工具来生成交互式文档。这些文档将提供给开发人员和用户,以便他们了解和使用你的API。 6. 测试API:Swagger还提供了一些工具来测试API。你可以使用Swagger UI来发送请求并查看响应数据。这样可以确保你的API按照预期工作,并帮助你发现和解决潜在的问题。 7. 生成客户端SDK和服务端存根代码:Swagger还可以根据API定义生成客户端SDK和服务端存根代码。这些代码将帮助开发人员更轻松地集成和使用你的API。 8. 部署和使用API:最后,你需要将API部署到服务器上,并让用户使用它。你可以将生成的文档和代码提供给用户,以便他们能够快速上手并开始使用你的API。 通过以上步骤,你可以从入门到精通地使用Swagger来设计、构建、文档化和使用RESTful Web服务。记得不断学习和实践,掌握更多高级功能和最佳实践,以提升你的Swagger技能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值