在现代 Java Web 开发中,Swagger 作为一款流行的 API 文档工具,极大地方便了前后端协作。然而,许多开发者在初次使用 Swagger 2.9.2 时,可能会遇到访问路径不正确、页面无法加载等问题。本文将详细介绍 Swagger 2.9.2 的默认访问地址、配置方式、常见问题及解决方案,并结合代码示例,帮助开发者快速上手。
在 Spring Boot + Swagger 2.9.2 项目中,默认的 Swagger UI 访问地址是:
http://localhost:8080/swagger-ui.html(假设服务运行在 8080 端口,若端口不同,需替换为实际端口。)
UI 类型 | 访问路径 | 适用场景 |
|---|---|---|
默认 Swagger UI | http://{host}:{port}/swagger-ui.html | 标准 Swagger 界面 |
Knife4j(增强版) | http://{host}:{port}/doc.html | 更友好的 UI,支持离线文档 |
Swagger Bootstrap UI | http://{host}:{port}/swagger-ui/index.html | 旧版 UI,较少使用 |
如果项目配置了 server.servlet.context-path(如 /api),则 Swagger 访问路径会变为:
http://localhost:8080/api/swagger-ui.html示例配置(application.yml):
server:
servlet:
context-path: /api确保 pom.xml 包含以下依赖:
<!-- Swagger 核心依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger UI 依赖(提供可视化界面) -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>创建一个 SwaggerConfig 类,并添加 @EnableSwagger2 注解:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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 api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller")) // 指定扫描的包
.paths(PathSelectors.any())
.build();
}
}启动应用后,访问:
http://localhost:8080/v2/api-docs如果返回 JSON 格式的 API 数据,说明 Swagger 配置成功。
swagger-ui.html 返回 404可能原因:
springfox-swagger-ui 依赖。解决方案:
检查依赖:确保 pom.xml 包含 springfox-swagger-ui。
配置 Spring Security 放行 Swagger(示例):
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(
"/swagger-ui.html",
"/v2/api-docs",
"/swagger-resources/",
"/webjars/"
);
}检查 @EnableSwagger2 和 Docket 配置,确保 basePackage 正确。
可能原因:
解决方案:
强制刷新页面(Ctrl + F5)。
检查浏览器控制台,查看是否有资源加载失败。
手动访问 Swagger 资源路径,如:
http://localhost:8080/webjars/springfox-swagger-ui/swagger-ui.css如果返回 404,可能是 Spring Boot 静态资源路径问题。
在 Docket 配置中添加 apiInfo:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API 文档")
.description("Spring Boot + Swagger 示例")
.version("1.0")
.contact(new Contact("作者", "https://example.com", "contact@example.com"))
.build();
}如果默认 Swagger UI 不够友好,可以改用 Knife4j(基于 Swagger 的增强版):
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>http://localhost:8080/doc.htmlKnife4j 提供更美观的界面,并支持离线文档导出。
本文详细介绍了 Swagger 2.9.2 的访问方式、配置方法及常见问题排查,主要内容包括:
http://localhost:8080/swagger-ui.html。@EnableSwagger2 + Docket。希望本文能帮助你顺利集成 Swagger,提升 API 开发效率!