SpringBoot整合Swagger,访问Swagger-ui.html导致404问题

错误复盘:

1、导入jar包

<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>3.0.0</version>
</dependency>
<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger-ui</artifactId>
     <version>3.0.0</version>
</dependency>

2、配置Swagger

package com.lpf.tools;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger配置类
 */
@Configuration
@EnableSwagger2
@ComponentScan(basePackages = "com.lpf.controller")
public class MyAPIConfig {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo apiInfo(){
        Contact contact = new Contact("个人", "localhost:8082", "xxxx@qq.com");
        return new ApiInfoBuilder()
                .title("文档标题")
                .description("文档描述")
                .contact(contact)
                .build();
    }
}

上面两步没错

3、直接访问localhost:8080/项目名(如果配置了的话)/swagger-ui.html

出现404错误

解决方案:

404:要么地址错误,要么文件不存在

地址错误情况:由于访问的是swagger-ui.html,如果你看的是教学视频,大概是访问这个链接,暂时认为是正确的。

文件不存在情况:查看swagger的ui包,如下

并不存在swagger.html这个文件,所以暂时认为是版本较高导致与之前的访问方式不一致。

4、在网上找原因查找了一会,发现一个包

<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-boot-starter</artifactId>
     <version>3.0.0</version>
</dependency>

导包后,查看

public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/');
        registry.addResourceHandler(new String[]{baseUrl + "/swagger-ui/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/springfox-swagger-ui/"}).resourceChain(false);
    }

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(this.baseUrl + "/swagger-ui/").setViewName("forward:" + this.baseUrl + "/swagger-ui/index.html");
    }

通过这段代码可以知道,swagger访问方式发生了改变,不再是之前版本的swagger-ui.html;我们访问/swagger-ui/xx的资源,才会转到访问/META-INF/resources/webjars/springfox-swagger-ui/文件夹下的资源!,这个文件帮我们在mvc中加了静态资源的过滤。

正确的访问连接应该是:localhost:8080/项目名(如果配置了的话)/swagger-ui/index.html

自此访问swagger成功。

404的原因是访问路径改变和访问的文件名也与之前版本不一致!

 

根本原因是我们需要配置正确的访问方式和资源映射方式:

回归到之前导入两个包的情况,没有了starter帮我们配置访问路径额和资源映射:

那我们需要自己配置一下mvc

package com.lpf.tools;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * mvc配置文件
 *
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    /**
     *
     * 页面路由功能
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //此处也可以不配置,直接访问index.html
        registry.addViewController("/swagger-ui/swagger.html").setViewName("forward:/swagger-ui/index.html");
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    }
}

这样我们也能通过localhost:8080/项目名(如果配置了的话)/swagger-ui/index.html访问swagger

注意:路径中必须存在/swagger-ui/  不然会导致swagger页面空白!

通过浏览器的Console窗口发现,原因是在swagger的js文件中设置了基本的路径中必须存在/swagger-ui/

必须存在该路径

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在Spring Boot项目中整合Swagger UI,可以按照以下步骤进行操作: 1. 在项目的Maven或Gradle配置中添加Swagger依赖,例如: ```xml <!-- Maven --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` ```groovy // Gradle implementation 'io.springfox:springfox-boot-starter:3.0.0' ``` 2. 创建一个配置类(例如`SwaggerConfig`)并添加`@Configuration`和`@EnableSwagger2`注解,示例如下: ```java import springfox.documentation.swagger2.annotations.EnableSwagger2; import org.springframework.context.annotation.Configuration; @Configuration @EnableSwagger2 public class SwaggerConfig { // 配置内容 } ``` 3. 在配置类中,可以使用`Docket`来配置Swagger的基本信息,例如设置API的标题、描述等,示例如下: ```java import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.RequestHandlerSelectors; import org.springframework.context.annotation.Bean; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controllers")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API Documentation") .description("API Documentation for my project") .version("1.0.0") .build(); } } ``` 4. 启动项目,访问`http://localhost:8080/swagger-ui.html`即可查看Swagger UI界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值