我是靠谱客的博主 无辜橘子,最近开发中收集的这篇文章主要介绍springboot(2.7.8)整合swagger,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、步骤

1.添加依赖


<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的config

package com.example.demo.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
@EnableSwagger2
public class SwaggerConfig {
/**
* 配置Swagger2相关的bean
*/
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//com包下所有API都交给Swagger2管理
.apis(RequestHandlerSelectors.basePackage("com"))
.paths(PathSelectors.any()).build();
}
/**
* 此处主要是API文档页面显示信息
*/
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("演示项目API")
.description("演示项目")
//
.version("1.0")
.build();
}
}

3.重启项目 访问地址:

http://localhost:8080/swagger-ui/index.html

二、问题及解决方案

1.项目启动报错

        解决:(1)启动类上加上@EnableWebMvc注解

                   (2)配置中添加

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

(上述解决方式二选一)

2.访问swagger-ui报404

        解决:删除原来的swagger2和swagger-ui依赖,改成spring-boot-starter依赖

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

最后

以上就是无辜橘子为你收集整理的springboot(2.7.8)整合swagger的全部内容,希望文章能够帮你解决springboot(2.7.8)整合swagger所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部