我知道这是一个老问题,但也许这会帮助将来遇到类似问题的人。
我遵循了与您提到的类似的教程,并且毫无问题地使它工作。几周前,我发布了自己的文档,介绍如何在 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
.