swagger2访问地址
时间: 2024-03-15 09:28:22 浏览: 17
Swagger2 的访问地址一般为:http://{host}:{port}/{context-path}/swagger-ui.html,其中:
- {host}:主机名或 IP 地址;
- {port}:端口号;
- {context-path}:应用的上下文路径,如果没有设置则为空。
比如,如果应用的访问地址为 http://localhost:8080/myapp,则 Swagger2 的访问地址为 http://localhost:8080/myapp/swagger-ui.html。注意,需要在应用启动的时候,确保 Swagger2 的配置已经生效。
相关问题
swagger2访问地址404
swagger2访问地址404可能是由于未给swagger配置资源路径,其他插件配置了资源路径,Spring MVC 找不到swagger需要的CSS、JS等资源所致。解决方法如下:
1.在SwaggerConfig类中添加静态资源访问路径配置,如下所示:
```
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
```
2.在pom.xml文件中添加swagger-ui依赖,如下所示:
```
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.52.0</version>
</dependency>
```
3.重新启动应用程序,访问swagger-ui.html页面即可。
swagger2访问地址配置
Swagger2 是一个用于生成 RESTful API 文档的开源框架,它可以让我们方便地查看和测试 API 接口。下面是 Swagger2 访问地址的配置方式:
1. 引入 Swagger2 相关依赖:
```xml
<!-- Swagger2 相关依赖 -->
<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. 创建 Swagger2 配置类:
```java
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger2 接口文档示例")
.description("这是一个 Swagger2 接口文档示例")
.version("1.0.0")
.build();
}
}
```
3. 配置访问地址:
在应用启动类中添加 `@EnableSwagger2` 注解,并在 application.properties 或 application.yml 文件中添加以下配置:
```yaml
# 配置 Swagger2 访问地址
swagger:
enabled: true # 是否启用 Swagger2
title: Swagger2 API # API 标题
description: 这是一个 Swagger2 接口文档示例 # API 描述
version: 1.0.0 # API 版本号
base-package: com.example.demo.controller # 扫描的 API 包路径
```
访问地址为:`http://localhost:8080/swagger-ui.html`,其中 `localhost:8080` 为应用的访问地址。