【发布时间】:2022-01-20 10:35:27
【问题描述】:
我有非启动 spring mvc 应用程序。当我点击 swagger-ui.html 或 swagger-ui/.我收到 404 错误。
我的配置类:
SpringFoxConfig:
@Configuration
@EnableSwagger2
public class SpringFoxConfig {}
SpringConfig
@ComponentScan("com.leverx.internship_project")
@Configuration
@EnableWebMvc
public class SpringConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:src/main/resources");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:src/main/resources");
}
}
依赖关系:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
【问题讨论】:
-
您可以尝试仅将
<dependency><groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency>作为依赖项而不是您提供的两个吗?此外,您需要通过 /swagger-ui/ 或 /swagger-ui/index.html 访问它。 swagger-ui.html 不再可用,afaik -
另外,我认为您不需要
@EnableSwagger2,也不需要定义addResourceHandlers。只需创建一个Docket类型的bean,例如@Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } -
@AndreiSfat 不起作用...
-
您是否在配置中使用了 server.servlet.context-path?正在使用什么?弹簧靴?
-
@AndreiSfat 我不使用spring boot,也没有使用任何配置,除了我发布的配置
标签: java spring spring-mvc swagger springfox