前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Swagger基本使用快速上手

Swagger基本使用快速上手

作者头像
陶然同学
发布2023-02-27 09:54:18
2600
发布2023-02-27 09:54:18
举报
文章被收录于专栏:陶然同学博客

Swagger简介

1、是一款让你更好的书写API文档规范且完整的框架。 2、提供描述、生产、消费和可视化RESTful Web Service。 3、是由庞大工具集合支撑的形式化规范。这个集合涵盖了从终端用户接口、底层代码库到商业API管理的方方面面。

Springboot集成Swagger

1.创建SpringBoot项目 引入Swagger坐标

需要引入两个Swagger坐标 分别是springfox-swagger2 springfox-swagger-ui

代码语言:javascript
复制
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
代码语言:javascript
复制
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

2.编写Swagger配置类

@Configuration 声明SwaggerConfiguration为配置类 @EnableSwagger2注解开启Swagger2 先配置Swagger信息 再配置Swagger的Docket的bean实例

代码语言:javascript
复制
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    //配置Swagger的Docket的bean实例
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())  //配置Swagger信息
                .select()
                /**
                 * apis():指定扫描的接口
                 *  RequestHandlerSelectors:配置要扫描接口的方式
                 *       basePackage:指定要扫描的包
                 *       any:扫面全部
                 *       none:不扫描
                 *       withClassAnnotation:扫描类上的注解(参数是类上注解的class对象)
                 *       withMethodAnnotation:扫描方法上的注解(参数是方法上的注解的class对象)
                 */
                .apis(RequestHandlerSelectors.basePackage("com.czxy"))
                /**
                 * paths():过滤路径
                 *  PathSelectors:配置过滤的路径
                 *      any:过滤全部路径
                 *      none:不过滤路径
                 *      ant:过滤指定路径:按照按照Spring的AntPathMatcher提供的match方法进行匹配
                 *      regex:过滤指定路径:按照String的matches方法进行匹配
                 */
                .paths(PathSelectors.any())
                .build();
    }

    //配置Swagger信息
    private ApiInfo apiInfo(){
        return new ApiInfo(
                "陶然同学。",
                "我的Swagger API文档",
                "1.0",
                "https://blog.csdn.net/weixin_45481821?spm=1018.2226.3001.5343",
                new Contact("陶然同学。","https://blog.csdn.net/weixin_45481821?spm=1018.2226.3001.5343","71829230@qq.com"),
                "Apache 2.0",
                "https://blog.csdn.net/weixin_45481821?spm=1018.2226.3001.5343",
                new ArrayList<VendorExtension>()
        );
    }
}

3.编写Controller

代码语言:javascript
复制
@RestController
public class HelloController {

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

4.测试进入Swagger页面

路径:http://localhost:8080/swagger-ui.html

常用注解

@ApiModel:为类添加注释

@ApiModelProperty:为类属性添加注释

@Api:为Controller添加注释

@ApiOperation:为Controller方法添加注释

@ApiParam:为Controller方法参数添加注释

测试Swagger

编写Controller测试传参

代码语言:javascript
复制
    @PostMapping("/username")
    public User getUserName(User user){
        return user;
    }

点击try it out 填写用户名 和 密码 再点击Execute发送 下面就能看到返回的结果

参考文档

Swagger官网:API Documentation & Design Tools for Teams | SwaggerSimplify API development for users, teams, and enterprises with our open source and professional toolset. Find out how Swagger can help you and get started today.

https://swagger.io/

总结

        Swagger不用深入学习  会基本的使用就可以了 配置类不懂也没关系 粘贴复制就行

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Swagger简介
  • Springboot集成Swagger
  • 1.创建SpringBoot项目 引入Swagger坐标
  • 2.编写Swagger配置类
    • 3.编写Controller
      • 4.测试进入Swagger页面
      • 常用注解
      • 测试Swagger
      • 参考文档
      • 总结
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档