Swagger 简单上

Swagger 简单上手

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务(https://swagger.io/)。 它的主要作用是:

  1. 使得前后端分离开发更加方便,有利于团队协作

  2. 接口的文档在线自动生成,降低后端开发人员编写接口文档的负担

  3. 功能测试

    Spring已经将Swagger纳入自身的标准,建立了Spring-swagger项目,现在叫Springfox。通过在项目中引入Springfox ,即可非常简单快捷的使用Swagger。

knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案,前身是swagger-bootstrap-ui,取名kni4j是希望它能像一把匕首一样小巧,轻量,并且功能强悍!

目前,一般都使用knife4j框架。

使用步骤

  1. 导入 knife4j 的 maven 坐标
<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-boot-starter -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>
  1. 配置类中加入 knife4j 的 相关配置, 使得 knife4j 生效
/**
     * 通过knife4j生成接口文档
     * @return
*/
    @Bean
    public Docket docket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("苍穹外卖项目接口文档")
                .version("2.0")
                .description("苍穹外卖项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sky.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
  1. 配置类中设置静态资源映射,否则接口文档页面无法访问(请求被 spring MVC 框架 拦截)
/**
     * 通过knife4j生成接口文档
     * @return
*/
    @Bean
    public Docket docket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("苍穹外卖项目接口文档")
                .version("2.0")
                .description("苍穹外卖项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sky.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

配置类如下

/**
 * 配置类,注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    /**
     * 通过knife4j生成接口文档
     * @return
     */
    @Bean
    public Docket docket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("苍穹外卖项目接口文档")
                .version("2.0")
                .description("苍穹外卖项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sky.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 设置静态资源映射
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

常用注解

通过注解可以控制生成的接口文档,使接口文档拥有更好的可读性,常用注解如下:

注解 说明
@Api 用在类上,例如Controller,表示对类的说明
@ApiModel 用在类上,例如entity、DTO、VO
@ApiModelProperty 用在属性上,描述属性信息
@ApiOperation 用在方法上,例如Controller的方法,说明方法的用途、作用

最后

打开以下链接,即可查看相应内容

localhost:8080/doc.html

相关推荐

  1. Swagger 简单

    2024-04-05 11:40:05       37 阅读
  2. <span style='color:red;'>Swagger</span>

    Swagger

    2024-04-05 11:40:05      37 阅读
  3. Swagger2

    2024-04-05 11:40:05       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 11:40:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 11:40:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 11:40:05       82 阅读
  4. Python语言-面向对象

    2024-04-05 11:40:05       91 阅读

热门阅读

  1. 每日一题 六十九期 洛谷 回文日期

    2024-04-05 11:40:05       39 阅读
  2. 数据库更新两张相关联的表

    2024-04-05 11:40:05       34 阅读
  3. 【leetcode】向字符串添加空格

    2024-04-05 11:40:05       31 阅读
  4. 2024.3.17力扣每日一题——最小高度树

    2024-04-05 11:40:05       33 阅读
  5. Apache Spark 的基本概念和在大数据分析中的应用

    2024-04-05 11:40:05       42 阅读
  6. WPF如何使用 System.Windows.Forms.FolderBrowserDialog

    2024-04-05 11:40:05       33 阅读
  7. 找出字符串中所有偶数的个数

    2024-04-05 11:40:05       36 阅读
  8. 单例模式的多种写法

    2024-04-05 11:40:05       39 阅读