Swagger的使用(第一个案例)

简介: Swagger的使用(第一个案例)

0x00 教程内容




0x01 配置Swagger



1. 添加 Swagger 依赖

(1)在pom.xml文件中导入 swagger 依赖:

<!-- swagger依赖 -->
<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. 配置 Swagger

(1)新建一个Swagger2Config ,添加上相关的配置

package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
 * @Auther: shaonaiyi@163.com
 * @Date: 2021/1/6 11:51
 * @Description: Swagger2配置类
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("邵奈一-Swagger教学")
                .description("邵奈一-Swagger教学-Restful API")
                .termsOfServiceUrl("http://127.0.0.1:8080/")
                .contact("邵奈一")
                .version("1.0")
                .build();
    }
}


(2)代码解释:

主要包含了两个方法,createRestApi()和apiInfo(),而后者返回的内容实际是个前者使用的。


1、createRestApi()

返回的是 Docket 类型,是固定的写法,大家不用关心。在方法内部,使用匿名内部类的方式实例化了一个 Docket 对象并返回,DocumentationType 即文档类型的选择我们需要根据集成的 Swagger 的版本来选择,这里选择 SWAGGER_2 表示使用的 Swagger 是2.X系列版本。


apis() 方法里面通过 RequestHandlerSelectors.basePackage() 属性来描述我们的目标包,就是我们项目中接口所在包的完整目录名称,这样 Swagger 就可以扫描到了,如果不配置此项,Swagger 是扫描不到我们项目中的接口的。

paths() 方法就是规定将我们项目中所有接口的请求路径都暴露给 Swagger 来生成 Swagger-UI 界面。

build() 方法就是将我们设置的上述参数都放到返回的 Docket 实例中。

2、apiInfo()

返回 Swagger-ApiInfo 类型,即返回 Swagger-UI 界面的基本信息。在方法内部也是通过匿名内部类的方式返回一个 ApiInfo 实例。


title() 方法:就是来规定我们的 Swagger-UI 界面的大标题。

description() 方法:就是来规定对 Swagger-UI 界面的一些简单描述信息。

contact() 方法:就是来规定创建 Swagger-UI 的作者的名称,目前已被废弃。

version() 方法:就是来规定 Swagger-UI 界面上所有接口的版本。

build() 方法:就是将我们设置的上述参数都放到返回的 ApiInfo 实例中。


0x02 检验Swagger


1. 启动项目

(1)访问端口:http://localhost:8080/swagger-ui.html#/,可以看到界面。


image.png


0x03 第一个例子


1. 添加注解

项目参考文章:SpringBoot+Thymeleaf+ECharts实现大数据可视化(基础篇)

主要是添加注解,完整代码如下:


package com.example.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Auther: shaonaiyi@163.com
 * @Date: 2020/8/23 23:10
 * @Description: UserController控制器
 */
@Api(tags = "用户管理")
@RestController()
@RequestMapping(value = "/user/")
public class UserController {
    @ApiOperation(value = "用户登录",notes = "必须使用post方法")
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    public String login(){
        return "login";
    }
}


image.png


2. 查看结果

访问:http://localhost:8080/swagger-ui.html#,然后点开用户管理,可以看到内容:

微信图片_20220619174840.png


0xFF 总结


  1. 本教程主要是介绍Swagger的配置与简单使用,具体的内容,还是需要结合实际项目才能解释得清。
相关文章
支付系统----微信支付16----创建案例项目-引入Swagger
支付系统----微信支付16----创建案例项目-引入Swagger
支付系统---微信支付14----创建案例项目---介绍,第二步引入Swagger,接口文档和测试页面生成工具,定义统一结果的目的是让结果变得更加规范,以上就是谷粒项目的几个过程
支付系统---微信支付14----创建案例项目---介绍,第二步引入Swagger,接口文档和测试页面生成工具,定义统一结果的目的是让结果变得更加规范,以上就是谷粒项目的几个过程
|
11月前
|
前端开发 Java API
深入剖析 Swagger enum:实际案例详解
enum 是 Swagger 规范中用来定义枚举类型的一种方式。它允许开发者在 API 文档中明确列出该接口的参数、返回值或请求体中可接受的枚举值。通过使用 Swagger enum,开发者可以更清晰地描述 API 的输入和输出,提高 API 文档的可读性和可维护性。
|
Java API Spring
SpringBoot开发案例之整合Swagger篇
前段时间整合过的一个支付服务,由于使用了Spring Boot快速开发,但是又懒得写详细的文档介绍,便顺手就把Swagger整合进来了,对支付服务进行分组API展示,如上图。
6673 0
|
3月前
|
数据可视化 Java API
Spring Boot与Swagger的集成
Spring Boot与Swagger的集成
|
3月前
|
Java API 开发者
在Spring Boot中集成Swagger API文档
在Spring Boot中集成Swagger API文档
|
9天前
|
前端开发 Java Spring
【非降版本解决】高版本Spring boot Swagger 报错解决方案
【非降版本解决】高版本Spring boot Swagger 报错解决方案
|
8天前
|
Java Spring
springboot 集成 swagger 2.x 和 3.0 以及 Failed to start bean ‘documentationPluginsBootstrapper‘问题的解决
本文介绍了如何在Spring Boot项目中集成Swagger 2.x和3.0版本,并提供了解决Swagger在Spring Boot中启动失败问题“Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerEx”的方法,包括配置yml文件和Spring Boot版本的降级。
springboot 集成 swagger 2.x 和 3.0 以及 Failed to start bean ‘documentationPluginsBootstrapper‘问题的解决
|
2月前
|
Java API Spring
springboot集成swagger
这篇文章介绍了如何在Spring Boot项目中集成Swagger 2.10.0来生成API文档,包括添加依赖、编写配置类、创建接口文档,并使用Knife4j美化Swagger界面。
|
3月前
|
JSON 缓存 Java
Spring Boot集成 Swagger2 展现在线接口文档
本节课详细分析了 Swagger 的优点,以及 Spring Boot 如何集成 Swagger2,包括配置,相关注解的讲解,涉及到了实体类和接口类,以及如何使用。最后通过页面测试,体验了 Swagger 的强大之处,基本上是每个项目组中必备的工具之一,所以要掌握该工具的使用,也不难。