1.导入maven依赖因为SpringBoot2.6.4最新版集成swagger有版本问题所以使用最新版
导入SpringBoot的web模块只需要导入一个maven
2.添加配置类io.springfox springfox-boot-starter 3.0.0
package com.sky.swagger.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
}
3.主启动类上面配置@EnableOpenApi
package com.sky.swagger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.oas.annotations.EnableOpenApi;
@SpringBootApplication
@EnableOpenApi
public class SwaggerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerDemoApplication.class, args);
}
}
4.测试
访问 http://localhost:8080/swagger-ui.html
访问 http://localhost:8080/swagger-ui/index.html



