问题描述
我正在尝试访问 swagger ui 页面,但是当我要访问该页面时,出现此错误
此应用程序没有明确的 /error 映射,因此您将其视为后备
swagger Config 类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.aluno"))
.paths(regex("/api.*"))
.build()
.apiInfo(MetaInfo());
}
private ApiInfo MetaInfo() {
ApiInfo apiInfo = new ApiInfo(
"Cursos API REST","API REST de registro de alunos.","1.0","Terms of Service",new Contact("Romulo Sorato","https://www.linkedin.com/in/r%C3%B4mulo-sorato-domingos-a6524437/","romulosorato@hotmail.com"),"Apache License Version 2.0","https://www.apache.org/licesen.html",new ArrayList<>()
);
return apiInfo;
}
}
我的 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
为什么我无法访问 swagger-ui?
看起来连 tomcat 都在启动。 我在 pom.xml 上添加了正确的依赖
解决方法
看来您要使用 Springfox3。如果是,请尝试以下更改。
移除 springfox-swagger2 和 springfox-swagger-ui 并在 pom.xml 中添加以下依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
SwaggerConfig - 删除 @EnableSwagger2
注释
@Configuration
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.aluno"))
.paths(PathSelectors.any())
.build()
.apiInfo(metaInfo());
}
private ApiInfo metaInfo() {
ApiInfo apiInfo = new ApiInfo(
"Cursos API REST","API REST de registro de alunos.","1.0","Terms of Service",new Contact("Romulo Sorato","https://www.linkedin.com/in/r%C3%B4mulo-sorato-domingos-a6524437/","romulosorato@hotmail.com"),"Apache License Version 2.0","https://www.apache.org/licesen.html",new ArrayList<>()
);
return apiInfo;
}
}
对于此版本,UI 的 URL 已更改如下:
http://localhost:8080/swagger-ui/index.html
如果您希望使用旧版本,那么只需使用版本 2.9.2
(对我来说非常好)。无需在您的配置文件或 UI 的 URL 中进行其他更改。
<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>
,
你能试试这个吗,它对我有用
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.aluno.controller"))
.paths(PathSelectors.any())
.build();
}
能否请您检查一下,这些依赖项已添加
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>