爱码网专注于资源免费下载

Swagger和Knife4j的使用

Jking2023-03-21 11:05:19641技术文章SwaggerKnife4j使用

一.Swagger简介

Swagger是一组围绕OpenAPI规范构建的开源工具,可以帮助你设计、构建、记录和使用REST API。主要的Swagger工具包括:
Swagger Editor:基于浏览器的编辑器,你可以在这里编写OpenAPI规范。
Swagger UI:将OpenAPI规范渲染成交互式的API文档。
Swagger Codegen:从OpenAPI规范中生成服务器存根和客户端库。

二.SSM整合Swagger

    1.引入Maven

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

  2.添加SwaggerConfig

package cn.atwangjian.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author wangjian
 * @date 2020/5/25 20:01
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket userApi() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public RequestMappingInfoHandlerMapping requestMapping() {
        return new RequestMappingHandlerMapping();
    }

}

    3.springMvc.xml添加Swagger配置

<!--添加swagger配置-->
    <!-- 引入swagger相关 -->
    <bean class="cn.atwangjian.config.SwaggerConfig"/>
        <!--使用swagger -->
        <!--<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />-->
        <!--使用knife4j -->
        <mvc:resources mapping="doc.html" location="classpath:/META-INF/resources/" />
        <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
    <bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config"/>

三.Swagger效果

访问:http://IP:port/web-obj-name/swagger-ui.html

Swagger和Knife4j的使用-第1张图片

Swagger和Knife4j的使用-第2张图片

四.Swagger常用属性

@Api()    
用于类;表示标识这个类是swagger的资源    
tags–表示说明 
value–也是说明,可以使用tags替代 

@ApiOperation() 
用于方法;表示一个http请求的操作 
value用于方法描述 
notes用于提示内容 
tags可以重新分组

@ApiParam() 
用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等) 
name–参数名 
value–参数说明 
required–是否必填

@ApiModel()
用于类 ;表示对类进行说明,用于参数用实体类接收 
value–表示对象名 
description–描述 
都可省略 

@ApiModelProperty()
用于方法,字段; 表示对model属性的说明或者数据操作更改 
value–字段说明 
name–重写属性名字 
dataType–重写属性类型 
required–是否必填 
example–举例说明 
hidden–隐藏

五.Knife4j简介

knife4j是为集成Swagger生成Api文档的增强解决方案

六.Knife4j核心功能

  1. 文档说明
    knife4j的列出接口文档的说明,包括接口地址、请求方式、接口描述、请求类型、请求示例、请求参数、响应状态、响应示例等。

  2. 在线调试
    knife4j提供了强大的在线接口调试功能,自动解析当前接口参数,同时包含表单验证,调用参数可返回接口响应内容、Raw、Headers、Curl以及响应耗时等信息。帮助开发者在线调试,而不用通过其他三方工具测试接口是否正确。

七.SSM整合Knife4j

  1. maven中引入jar包
    正如knife4j官网介绍所说,如果你是一名Java开发工程师,那么使用swagger-bootstrap-ui将会非常简单,只需要在原使用的基础上,添加swagger-bootstrap-ui的maven引用jar包即可。

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>

  2.添加SwaggerConfig配置和springMvc配置
    同swagger篇

八.Knife4j效果

访问地址:http://IP:port/web-obj-name/doc.html
1.文档

Swagger和Knife4j的使用-第3张图片

knife4j为我们写好了接口文档,可以直接复制导出,非常的方便。
2.调试
调试成功:

Swagger和Knife4j的使用-第4张图片

调试报错都会显示:

Swagger和Knife4j的使用-第5张图片

本文链接:https://www.icode1024.com/article/14.html

网友评论