springboot项目中bSwagger

什么是Swagger?

  • Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务,是非常流行的API表达工具
  • 它可以将项目中所有想要暴露的接口展现在页面上,并且可以进行接口调用和测试。
  • Swagger的目标是使客户端和文件系统作为服务器以同样的速度来更新,文件的方法、参数和模型紧密集成到服务器端的代码,允许API始终保持同步。
  • 通过Swagger的注解功能,开发人员可以标注接口文档,包括接口名、请求方法、参数、返回信息等。
  • Swagger能够自动生成完善的RESTful API文档,,同时并根据后台代码的修改 同步更新,同时提供完整的测试页面来调试API。
  • Swagger遵循OpenAPI规范,是一个功能强大的API文档框架,可以大大简化API的开发和使用过程。

SpringBoot项目部署Swagger的操作

1、在pom.xml文件中加入相应的maven依赖(springfox-swagger2和springfox-swagger-ui

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

2、配置Swagger

进入src->main->java->项目包->config中新建Swagger2Config的java类

如下图所示

在Swagger2Config文件中写入以下代码

package cn.tedu.bookstore.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;

@Configuration    //告诉spring容器,这个类是一个配置类
@EnableSwagger2    //启用Swagger2功能
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // cn包下所有API都交给Swagger2管理      注意这个“cn”是第一个包名,因为我们所有代码都在这个包里边,选中这个包就选中了所有接口
                .apis(RequestHandlerSelectors.basePackage("cn"))
                .paths(PathSelectors.any()).build();
    }
    // API文档页面显示信息
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("项目于API")                     // 标题
                .description("Swagger2演示项目")   // 描述
                .version("1.0")                        //版本
                .build();
    }
}

3、修改配置文件

Spring Boot 2.6.x后与Swagger有版本冲突问题,需要在配置文件中加入以下配置:

(1)配置文件是application.properties

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

  (2)配置文件是application.yml

spring:
  mvc:                        #Swagger注解
    pathmatch:
      matching-strategy: ant_path_matcher

4、使用Swagger2进行接口测试

(1)启动SpringBoot项目

(2)在网址中输入:http://127.0.0.1:8050/swagger-ui.html(8050为端口号,根据自己的情况更改)

注意:如果设置了servlet的路径,则需要在网址中加入相应路径

如下在配置中添加了context-path,网址就变成了http://127.0.0.1:8050/bookstore/swagger-ui.html

server:
  port: 8050
  servlet:
    context-path: /bookstore

输入网址打开测试化页面:

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值