Swagger UI未在浏览器上呈现

问题描述

我已经向spring boot应用程序添加了大张旗鼓的依赖关系,并且JSON正在按预期加载。当我尝试通过调用此URL http://localhost:9090/swagger-ui.html加载UI时,浏览器上将显示以下错误。

Whitelabel Error Page

This application has no explicit mapping for /error,so you are seeing this as a fallback.

There was an unexpected error (type=Not Found,status=404).

pom.xml

 <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>

配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

Ps-我的应用程序在端口9090下运行

解决方法

我有类似的问题。确保您整理了招摇的物品。
   @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {


    //Swagger UI property
    registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
}

必须在实现的配置中使用“ WebMvcConfigurer”进行声明。

更多信息在这里:https://springfox.github.io/springfox/docs/current/

,

我找到了解决方案。从Swagger 3.0开始,我们不需要在构建工具中添加2个依赖项。可以替换springfox-boot-starter而不是那两个依赖项。

pom.xml

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

表格swagger 3.0的网址应为http://localhost:9090/swagger-ui/,而不是http://localhost:9090/swagger-ui.html

,

如果您定义了基本URL,例如/rest/api,则也需要输入该URL。 我的配置如下:

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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter{
    @Bean
    public Docket APIs() { 
        return new Docket(DocumentationType.SWAGGER_2)
          .apiInfo(metadata())
          .select()
          .apis(RequestHandlerSelectors.basePackage("<rest_controller_package>"))
          .paths(PathSelectors.any())
          .build();
    }
    
    private ApiInfo metadata() {
        return new ApiInfoBuilder()
            .title("<title_here>")
            .description("<description_here>")
            .version("BETA")
            .build();
    }
}

和pom.xml

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

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

我可以通过http://localhost:9090/api/swagger-ui.html

访问它 ,

我认为您应该在 SwaggerConfig 类中添加一个返回Docket对象的Bean,该对象指定要在swagger中显示的路径和包。像这样:

@Configuration 
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
          .select()
          .apis(RequestHandlerSelectors.basePackage(“com.spring.rest.controller”))
          .paths(PathSelectors.any())
          .build();
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...