Spring Boot 整合 Swagger 接口文档工具

我们在开发接口的时候,会将接口文档给前端的开发者进行对接。我们可以通过 Postman 或者 Yapi 等接口管理工具进行编写管理。实际开发中,接口的管理确实也应该通过专业的工具管理。

那么,如果只是小团队使用,我们是否可以在边开发的过程中,顺便把接口文档给写了呢?

当然,本文,我们就来谈谈怎么在 Spring Boot 整合 Swagger 接口文档工具。

本文开发环境:

  • spring boot 版本 2.1.3.RELEASE

  • java SDK 版本 1.8

  • mac m1 系统

本文,在笔者之前的项目 Spring Security 简单了解使用[1] 基础上开发。

笔者尝试了下整合 swagger3,但是因为原先项目版本的问题,未能整合成功。故整合 swagger2,文档作用都一样,就是页面长得不一样,可以放心使用。

添加依赖

我们在 pom.xml 中添加下面的依赖:

<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>
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>

并在配置文件中添加配置:

spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER

引入配置

在包 com.launch.config 中添加 SwaggerConfig.java 类:

package com.launch.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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.launch.controller")) 
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Launch 系统") 
                .description("Jimmy Control System") 
                .version("1.0.0") 
                
                .contact(new Contact("Jimmy", "https://juejin.cn/user/1996368846261294", "reng99@outlook.com"))
                .build();
    }

}

到此,我们运行项目,打开连接 http://localhost:8080/swagger-ui/index.html,咦,404 耶~

处理 404

版本的问题,使得我们无法读取 swagger 包下面的页面。那么,我们来重写。

我们在 com.launch.config 中新增 WebMvcConfig.java 文件:

package com.launch.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

    }
}

重新启动,访问路径 http://localhost:8080/doc.html,就可以看到效果。

在本文 Spring Security 简单了解使用[2] 中,我们已经开发好了六个接口。点击进入其中一个,比如 queryAll 查询所有用户的接口,可看到其文档:

我们还可以对该接口进行调试:

感兴趣的读者可以自行尝试。

参考

  • 《Spring Boot 实战派》

  • SpringBoot教程(十六) | SpringBoot集成swagger(全网最全)[3]

  • Swagger Configuration with Spring Boot 3 | Swagger + Spring Boot 3[4]

参考资料

[1]

https://juejin.cn/post/7261954917681283109: https://juejin.cn/post/7261954917681283109

[2]

https://juejin.cn/post/7261954917681283109: https://juejin.cn/post/7261954917681283109

[3]

https://lsqingfeng.blog.csdn.net/article/details/123678701: https://link.juejin.cn/?target=https%3A%2F%2Flsqingfeng.blog.csdn.net%2Farticle%2Fdetails%2F123678701

[4]

https://www.youtube.com/watch?v=Eo6v01KUeZM: https://link.juejin.cn/?target=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DEo6v01KUeZM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

技术小羊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值