无法通过 Spring Boot 获取 Swagger UI

2024-05-07

我正在关注这篇文章http://raibledesigns.com/rd/entry/documenting_your_spring_api_with http://raibledesigns.com/rd/entry/documenting_your_spring_api_with.

一切正常,但无法集成 Swagger UI。

http://localhost:8080/docs/index.html  

导致 /error 重定向。


我知道这是一个老问题,但也许这会帮助将来遇到类似问题的人。

我遵循了与您提到的类似的教程,并且毫无问题地使它工作。几周前,我发布了自己的文档,介绍如何在 Spring boot 项目中设置带有 UI 的 Swagger。也许它会对您有所帮助,因为它更短且更新。

添加 Maven 依赖项

将这些粘贴到您的 pom.xml 中:

<dependency>
 <groupId>com.mangofactory</groupId>
 <artifactId>swagger-springmvc</artifactId>
 <version>1.0.2</version>
 <type>jar</type>
</dependency>

添加 Swagger UI

下载招摇的用户界面 https://github.com/swagger-api/swagger-ui来自github。复制dist文件夹到你的webapp目录并重命名dist to swagger(或任何你喜欢的名字)。

打开索引.html文件并更改第一个 javascript 函数中的 url,使其指向/api-docs endpoint:

var url = window.location.search.match(/url=([^&]+)/);
  if (url && url.length > 1) {
  url = decodeURIComponent(url[1]);
  } else {
  url = "/project-name/api-docs";
 }

配置 Swagger

创建一个SwaggerConfig.java类并在那里配置 swagger:

@Configuration
@EnableSwagger
@EnableAutoConfiguration
public class SwaggerConfig {
  private SpringSwaggerConfig springSwaggerConfig;
  @Autowired
  public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
    this.springSwaggerConfig = springSwaggerConfig;
  }
  @Bean
  public SwaggerSpringMvcPlugin customImplementation() {
    return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
    // Root level documentation
    .apiInfo(new ApiInfo("Swagger-demo JSON API", "This service provides a JSON representation the service API", null, null, null, null))
    .useDefaultResponseMessages(false)
    // Map the specific URL patterns into Swagger
    .includePatterns("/greeting.*");
  }
}

你的招摇现在应该已经启动并运行了。尝试访问/project-name/swagger/index.html.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法通过 Spring Boot 获取 Swagger UI 的相关文章

随机推荐