Spring Boot集成Restful Api在线文档接口调试工具 Swagger

本文档介绍了如何使用Swagger2为SpringBoot项目生成RESTful API接口文档,包括环境准备、创建SpringBoot工程、引入Swagger依赖、编写控制器、配置Swagger信息,以及最终测试接口。Swagger有助于简化接口文档的维护,提供接口调试功能,提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

一、Swagger简介

Swagger的目标是为REST API 定义一个标准的,与语言无关的接口,使人和计算机在看不到源码或者看不到文档或者不能通过网络流量检测的情况下能发现和理解各种服务的功能。当服务通过Swagger定义,消费者就能与远程的服务互动通过少量的实现逻辑。类似于低级编程接口,Swagger去掉了调用服务时的很多猜测。

Swagger 是一个用于生成、描述和调用 RESTful 接口的 Web 服务。通俗的来讲,Swagger 就是将项目中所有(想要暴露的)接口展现在页面上,并且可以进行接口调用和测试的服务。

Swagger官网地址:https://swagger.io/

在这里插入图片描述

Swagger有什么用?

  • 将项目中所有的接口展现在页面上,这样后端程序员就不需要专门为前端使用者编写专门的接口文档;
  • 当接口更新之后,只需要修改代码中的 Swagger 描述就可以实时生成新的接口文档了,从而规避了接口文档老旧不能使用的问题;
  • 通过 Swagger 页面,我们可以直接进行接口调用,降低了项目开发阶段的调试成本。

在这里插入图片描述

二、环境准备

在开始开发之前,我们需要准备一些环境配置:

  • jdk 1.8 或其他更高版本
  • 开发工具 IDEA
  • 管理依赖 Maven

三、构建Spring Boot工程

打开idea -> file -> Nwe -> Project ,如图,勾选填写相关的配置信息:

在这里插入图片描述

勾选一些初始化的依赖配置:

在这里插入图片描述

工程搭建完成:

在这里插入图片描述

四、引入Swagger依赖

首先,我们要去 mvnrepository 查询 Swagger 的依赖

在这里插入图片描述

将依赖引入工程中:

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

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


五、编写一个Test控制器

package com.example.swagger_test.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaoZhao
 * @date 2022/10/24
 * @describe
 */
@RestController
public class TestController {

    @GetMapping("hello")
    public String hello(){
        return "hello swagger";
    }
}

在这里插入图片描述

在这里插入图片描述

六、配置Swagger

编写Swagger配置类:

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author xiaoZhao
 * @date 2022/10/24
 * @describe
 */
@Configuration
@EnableSwagger2 // 开启Swagger2
public class SwaggerConfig {
    // 使用默认配置
}

访问:http://localhost:8080/swagger-ui.html 进入Swagger管理页面

在这里插入图片描述

从上图可以看到,接口文档详细信息、接口信息等

配置Swagger信息:

package com.example.swagger_test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author xiaoZhao
 * @date 2022/10/24
 * @describe
 */
@Configuration
@EnableSwagger2 // 开启Swagger2
public class SwaggerConfig {

    // 配置Swagger Docket
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    // 配置Swagger信息
    private ApiInfo apiInfo(){
        // 作者信息
        Contact contact = new Contact("小赵", "https://blog.csdn.net/Zp_insist?type=blog", "test@qq.com");

        return new ApiInfo("测试 Swagger API",
                "一个工程用来测试Swagger的使用",
                "1.0",
                "https://blog.csdn.net/Zp_insist?type=blog",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());
    }
}

七、最终测试

重启工程进入Swagger管理页面

在这里插入图片描述

评论 37
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百思不得小赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值