Swagger UI在浏览器上无法渲染。

huangapple go评论68阅读模式
英文:

Swagger UI is not rendering on the browser

问题

我已经将 Swagger 依赖项添加到 Spring Boot 应用程序中,并且 JSON 正在按预期加载。当我尝试通过调用此 URL http://localhost:9090/swagger-ui.html 来加载 UI 时,在浏览器上显示以下错误。

Whitelabel Error Page

此应用程序没有针对 /error 的显式映射,因此您正在看到此作为回退。

发生了意外错误(类型=Not Found,状态=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>

config class

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

附注:我的应用程序在端口 9090 上运行。

英文:

I have added swagger dependencies to the spring boot application and JSON is loading as expected. When I try to load UI by calling this URL http://localhost:9090/swagger-ui.html then the following error is displayed on the browser.

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

 &lt;dependency&gt;
        &lt;groupId&gt;io.springfox&lt;/groupId&gt;
        &lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
        &lt;version&gt;3.0.0&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;io.springfox&lt;/groupId&gt;
        &lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
        &lt;version&gt;3.0.0&lt;/version&gt;
    &lt;/dependency&gt;

config class

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

Ps - My application is running under the port 9090

答案1

得分: 4

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

pom.xml

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

从 Swagger 3.0 开始,URL 应该是 http://localhost:9090/swagger-ui/,而不是 http://localhost:9090/swagger-ui.html

英文:

I found the solution. From Swagger 3.0 we don't need to add 2 dependencies into the build tool. springfox-boot-starter can be replaced instead of those 2 dependencies.

pom.xml

&lt;dependency&gt;
    &lt;groupId&gt;io.springfox&lt;/groupId&gt;
    &lt;artifactId&gt;springfox-boot-starter&lt;/artifactId&gt;
    &lt;version&gt;3.0.0&lt;/version&gt;
&lt;/dependency&gt;

Form swagger 3.0 the URL should be http://localhost:9090/swagger-ui/rather than http://localhost:9090/swagger-ui.html

答案2

得分: 1

我遇到过类似的问题。确保你声明了 Swagger 项目。

       @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
        //Swagger UI 属性
        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/

英文:

i had similar issue. Make sure you declerate the swagger items.

   @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {


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

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

This must be declareted in the config of implementation with "WebMvcConfigurer".

More information here : https://springfox.github.io/springfox/docs/current/

答案3

得分: 0

如果您定义了基本URL,例如 /rest/api,则您还需要将其添加进去。
我的配置如下所示:

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 进行访问。

英文:

If you have base url defined, for eg /rest, /api then you will need to put that as well.
My config looks like this:

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(&quot;&lt;rest_controller_package&gt;&quot;))
          .paths(PathSelectors.any())
          .build();
    }
    
    private ApiInfo metadata() {
        return new ApiInfoBuilder()
            .title(&quot;&lt;title_here&gt;&quot;)
            .description(&quot;&lt;description_here&gt;&quot;)
            .version(&quot;BETA&quot;)
            .build();
    }
}

and pom.xml

&lt;dependency&gt;
			&lt;groupId&gt;io.springfox&lt;/groupId&gt;
			&lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
			&lt;version&gt;2.9.2&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;io.springfox&lt;/groupId&gt;
			&lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
			&lt;version&gt;2.9.2&lt;/version&gt;
		&lt;/dependency&gt;

and I access it via http://localhost:9090/api/swagger-ui.html

答案4

得分: 0

我认为你应该在 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();
    }
}
英文:

I think you should add into the SwaggerConfig class a Bean that return a Docket object, which specify the paths and the packages that you want to show in swagger. Something like this:

@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();
    }
}

huangapple
  • 本文由 发表于 2020年9月17日 19:09:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63936779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定