zoukankan      html  css  js  c++  java
  • Swagger和knife4j简单应用

    Swagger和knife4j使用

    Swagger使用

    一、简介

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

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

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

    3. 功能测试

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

    二、SpringBoot集成Swagger

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
    </dependency>
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfiguration {
    
       @Bean
       public Docket buildDocket() {
          return new Docket(DocumentationType.SWAGGER_2)
                  .apiInfo(buildApiInfo())
                  .select()
                  // 要扫描的API(Controller)基础包
                  .apis(RequestHandlerSelectors.basePackage("com.stone"))
                  .paths(PathSelectors.any())
                  .build();
       }
    
       private ApiInfo buildApiInfo() {
          return new ApiInfoBuilder()
                  .title("stoneAPI文档")
                  .description("平台管理服务api")
                  .contact("stone")
                  .version("1.0.0").build();
       }
    }
    

    三、Swagger常用注解

    在Java类中添加Swagger的注解即可生成Swagger接口文档,常用Swagger注解如下:

    @Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口 @ApiParam:单个参数的描述信息

    @ApiModel:用对象来接收参数

    @ApiModelProperty:用对象接收参数时,描述对象的一个字段

    @ApiResponse:HTTP响应其中1个描述

    @ApiResponses:HTTP响应整体描述

    @ApiIgnore:使用该注解忽略这个API

    @ApiError :发生错误返回的信息

    @ApiImplicitParam:一个请求参数

    @ApiImplicitParams:多个请求参数的描述信息

    @ApiImplicitParam属性:

    属性 取值 作用
    paramType 查询参数类型
    path 以地址的形式提交数据
    query 直接跟参数完成自动映射赋值
    body 以流的形式提交 仅支持POST
    header 参数在request headers 里边提交
    form 以form表单的形式提交 仅支持POST
    dataType 参数的数据类型 只作为标志说明,并没有实际验证
    Long
    String
    name 接收参数名
    value 接收参数的意义描述
    required 参数是否必填
    true 必填
    false 非必填
    defaultValue 默认值

    四、测试

    通过路径/swagger-ui.html访问

    Knife4j使用

    一、简介

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

    gitee地址:https://gitee.com/xiaoym/knife4j

    官方文档:https://doc.xiaominfo.com/

    效果演示:http://knife4j.xiaominfo.com/doc.html

    二、springboot集成

    <dependency>
         <groupId>com.github.xiaoymin</groupId>
         <artifactId>knife4j-spring-boot-starter</artifactId>
        <version>2.0.2</version>
        //高版本可能会有冲突
    </dependency>
    
    @Configuration
    @EnableSwagger2
    @EnableKnife4j
    public class Swagger2Configuration {
    
          @Bean
       public Docket buildDocket() {
          return new Docket(DocumentationType.SWAGGER_2)
                  .apiInfo(buildApiInfo())
                  .select()
                  // 要扫描的API(Controller)基础包
                  .apis(RequestHandlerSelectors.basePackage("com.stone"))
                  .paths(PathSelectors.any())
                  .build();
       }
    
       private ApiInfo buildApiInfo() {
          return new ApiInfoBuilder()
                  .title("stoneAPI文档")
                  .description("平台管理服务api")
                  .contact("stone")
                  .version("1.0.0").build();
       }
    }
    

    三、测试

    通过路径/doc.html访问

  • 相关阅读:
    问题2017S03
    问题2017S02
    高等代数问题1
    无穷积分换元法的严格解释
    线性空间的同构理论
    问题2017S01
    朴素贝叶斯分类
    决策树
    温习MATLAB
    感知机
  • 原文地址:https://www.cnblogs.com/theStone/p/14668792.html
Copyright © 2011-2022 走看看