【SpringBoot】SpringBoot配置Swagger

前言

使用Swagger只需要按照规范去定义接口及接口的相关信息,就可以做到生成接口文档和在线接口调试页面
官网:Swagger官网
Knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案


配置步骤

1.导入knife4j的maven坐标(测试需要导入web依赖哦~)

<dependency>
	<groupId>com.github.xiaoymin</groupId>
	<artifactId>knife4j-spring-boot-starter</artifactId>
	<version>3.0.2</version>
</dependency>

2.再配置类中加入knife4j相关配置

3.设置静态资源映射,否则接口文档页面无法访问

package com.config;

import com.sky.interceptor.JwtTokenAdminInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
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;

/**
 * 配置类,注册web层相关组件
 */
@Configuration
@Slf4j  //日志门面
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    /**
     * 通过knife4j生成接口文档
     * 我这里提供了日志功能,不需要的可以删除
     * 这里是第二步
     */
    @Bean
    public Docket docket() {
        log.info("开始注册knife4j接口文档...");  //输出日志
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("XXX接口文档")
                .version("XXX")
                .description("XXX接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                //扫描规则,当前包及其子包
                .apis(RequestHandlerSelectors.basePackage("扫描包的路径名")) 
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 设置静态资源映射
     * 这里是第三步,设置访问页面
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始注册静态资源映射...");
        //在这里配置访问页面,不修改访问路径为:localhost:8080/doc.html
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

4.访问localhost:8080/doc.html,即可访问成功

使用步骤

使用Swagger只需要再类上添加注解即可

在这里插入图片描述
可以添加这些注解,为里面的属性赋值,得到的页面会有信息提示,提高可读性

总结

本文我们学会了如何配置Swagger来对后端接口进行测试,其中配置的代码不需要会写,只需要更改为自己的即可

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无技术,不人生

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

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

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

打赏作者

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

抵扣说明:

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

余额充值