库里斯托 2024-05-07 23:24 采纳率: 87.6%
浏览 8

Springboot集成Swagger报404怎么办?

img

img


项目是正常启动的
swagger2和swaggerui都是3.0.0的
swagger配置类

package com.easytest.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author KingLee
 * @date 2020-11-10
 * @since 1.0
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.mywind.integrated"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("集成接口")
                .description("创建人:xxx")
                .version("1.0")
                .build();
    }
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

  • 写回答

5条回答 默认 最新

  • GISer Liu 2024-05-08 07:07
    关注

    该回答引用自GPT-3.5,由博主GISer Liu编写:

    根据你提供的代码和描述,出现404错误可能是由于Swagger的资源路径配置不正确所致。以下是我针对你的问题提出的解决思路和方案:

    问题分析

    1. 404错误 : 404错误通常表示资源未找到,这可能是由于Swagger UI的资源路径配置不正确导致的。
    2. Swagger配置类 : 你提供了Swagger的配置类,但是404错误表明Swagger UI页面无法正确访问。

      解决思路

    3. 检查资源路径 : 确保Swagger UI页面和相关资源的路径配置正确。
    4. 确认Swagger UI是否正确启用 : 确保Swagger UI已经被正确启用,并且配置类中的路径与实际路径一致。

      解决方案

    5. 检查Swagger UI路径配置 : 确保Swagger UI页面的路径配置正确。根据你提供的配置类,Swagger UI页面应该在"/swagger-ui.html"处可访问,而相关资源应该在"/webjars/**"路径下可访问。
    6. 确认Swagger是否启用 : 确保Swagger已经被正确启用。可以通过检查Spring Boot启动时的日志来确认Swagger是否成功初始化。

      修改代码

      根据你的配置类,可能需要对addResourceHandlers方法中的路径进行调整,以确保Swagger UI页面和相关资源可以正确访问。
      java
      Copy code
      @Override
      protected void addResourceHandlers(ResourceHandlerRegistry registry) {
      // 解决静态资源无法访问
      registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
      // 解决swagger-ui.html无法访问
      registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
      // 解决swagger的js文件无法访问
      registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
      }
      

      总结

      根据你的描述和提供的代码,出现404错误可能是由于Swagger UI页面的路径配置不正确导致的。确保Swagger
      UI页面和相关资源的路径配置正确,并且Swagger已经被正确启用,即可解决该问题。

    如果该回答解决了您的问题,请采纳!如果没有,请私信联系或评论您的疑惑

    评论

报告相同问题?

问题事件

  • 创建了问题 5月7日

悬赏问题

  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错