我想为我的spring Boot 应用程序设置swagger ui,但当我转到http://localhost:8080/swagger-ui
时,我得到了404未找到。我尝试了http://localhost:8080/swagger-ui.html
和http://localhost:8080/swagger-ui/index.html
,但仍然得到了404。我看了无数的教程和表格问题,但问题仍然存在。下面是我的依赖项和swagger配置文件
build.gradle:
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.10'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.company'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.modelmapper:modelmapper:2.1.1'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
}
SwaggerConfig.java:
package com.company.app.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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
@EnableWebMvc
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.company"))
.paths(PathSelectors.ant("/api/**"))
.build();
}
}
我假设这是关于版本的,如果是关于swagger的版本,我应该使用哪个版本来使用我正在使用的spring Boot 版本?
1条答案
按热度按时间t2a7ltrp1#
我通过修改
SwaggerConfig.java
解决了这个问题,我在SwaggerConfig
中扩展了WebMvcConfigurerAdapter
并覆盖了void addResourceHandlers(ResourceHandlerRegistry registry)
: