Swagger接口文档配置

注意配置swagger的版本和本地springboot版本的兼容性

我这里用的是springboot-2.7.12  swagger-3.0.0

如何查看本地springboot版本

1.pom.xml引入依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

2. applicationproperties 添加配置参数

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

3.启动类加@EnableOpenApi注解

常见错误

   注解爆红:引入依赖后,没有刷新,点击右侧maven工具栏,找到项目的生命周期,先点击"clean"清除后点击"install" 

4.创建配置类

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;
@Configuration
@EnableOpenApi
public class SwaggerConfiguration {
    @Bean
    public Docket createRestApi(){
        System.out.println("---------------swaggerTest---------------");
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();

    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("Swagger3接口文档")
                .description("测试的接口文档")
                .version("1.0")
                .build();
    }
    @Bean
    public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
        return new BeanPostProcessor() {
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof WebMvcRequestHandlerProvider) {
                    customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
                }
                return bean;
            }

            private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(
                    List<T> mappings) {
                List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null)
                        .collect(Collectors.toList());
                mappings.clear();
                mappings.addAll(copy);
            }

            @SuppressWarnings("unchecked")
            private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
                try {
                    Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
                    field.setAccessible(true);
                    return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
                }
                catch (IllegalArgumentException | IllegalAccessException e) {
                    throw new IllegalStateException(e);
                }
            }
        };
    }
}

5.创建测试接口

@Api 标识方法swagger可以扫描

@ApiOpertion 描述接口功能和参数,这样前端自己可以根据描述调用接口

@Api("测试接口")
@RestController//当前类交给springboot管理,并且自动对返回值进行转换
@RequestMapping("/hello")
public class UserController{

    @ApiOperation("测试swagger功能")
    @GetMapping("/testResult")
    public BaseResult testNews(Integer num) {
        Thread thread = new Thread(() -> {
            List<User> list = new ArrayList<>();
            try {
                Thread.sleep(5000);
                System.out.println(Thread.currentThread().getName()+":接口文档测试成功");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"myTestThread");
        thread.start();
        return BaseResult.success("开始循环");
    }

6.启动项目,访问http://localhost:8088/swagger-ui/index.html

 注意端口号该为自己配置的端口号,我本地是8088

怎么配置 》》application.properties 添加 server.port=8088

 

7.测试发送请求

点击“try it out" >> "输入请求参数">>点击"execute"

接口返回结果正常,看下控制台,成功创建线程并打印

就这些,感谢你们阅读 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

先锋 Coder

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

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

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

打赏作者

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

抵扣说明:

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

余额充值