无法通过弹簧启动获得Swagger UI

Abh*_*ami 7 swagger-ui spring-boot

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

一切正常但无法集成Swagger UI.

http://localhost:8080/docs/index.html  
Run Code Online (Sandbox Code Playgroud)

导致/错误重定向.

Sma*_*ajl 2

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

我遵循了与您提到的类似的教程,并且毫无问题地使它工作。几周前,我发布了自己的文档,介绍如何在 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>
Run Code Online (Sandbox Code Playgroud)

添加 Swagger UI

从 github下载Swagger UI。将文件夹复制dist到您的webapp目录中并重命名distswagger(或您喜欢的任何名称)。

打开复制目录中的index.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";
 }
Run Code Online (Sandbox Code Playgroud)

配置 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.*");
  }
}
Run Code Online (Sandbox Code Playgroud)

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