Springfox(Swagger 规范 2.0,当前)
Springfox 已取代 Swagger-SpringMVC,现在支持 Swagger 规范 1.2 和 2.0。实现类已更改,允许进行更深入的自定义,但需要进行一些工作。 documentation 已改进,但仍需要添加一些细节以进行高级配置。 1.2 实现的旧答案仍然可以在下面找到。
Maven 依赖项
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
最低限度的实现看起来或多或少相同,但现在使用 Docket 类而不是 SwaggerSpringMvcPlugin 类:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("/api/.*"))
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("TITLE")
.description("DESCRIPTION")
.version("VERSION")
.termsOfServiceUrl("http://terms-of-services.url")
.license("LICENSE")
.licenseUrl("http://url-to-license.com")
.build();
}
}
您的 Swagger 2.0 API 文档现在可以通过 http://myapp/v2/api-docs 获得。
注意:如果你没有使用 Spring boot,那么你应该添加 jackson-databind 依赖。由于springfox使用jackson进行数据绑定。
现在添加 Swagger UI 支持变得更加容易。如果您使用的是 Maven,请为 Swagger UI webjar 添加以下依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
如果您使用的是 Spring Boot,那么您的 Web 应用程序应自动获取必要的文件并在 http://myapp/swagger-ui.html(以前为:http://myapp/springfox)处显示 UI。如果您不使用 Spring Boot,那么正如 yuriy-tumakha 在下面的答案中提到的那样,您将需要为文件注册一个资源处理程序。 Java 配置如下所示:
@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
static documentation generation 的新功能看起来也很不错,虽然我自己还没有尝试过。
Swagger-SpringMVC(Swagger 规范 1.2,更早版本)
Swagger-SpringMVC 的文档可能有点混乱,但实际上设置起来非常容易。最简单的配置需要创建一个 SpringSwaggerConfig bean 并启用基于注释的配置(您可能已经在 Spring MVC 项目中这样做了):
<mvc:annotation-driven/>
<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
但是,我认为使用 SwaggerSpringMvcPlugin 而不是以前的 XML 定义的 bean 定义自定义 Swagger 配置的额外步骤是值得的:
@Configuration
@EnableSwagger
@EnableWebMvc
public class SwaggerConfig {
private SpringSwaggerConfig springSwaggerConfig;
@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
this.springSwaggerConfig = springSwaggerConfig;
}
@Bean
public SwaggerSpringMvcPlugin customImplementation(){
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo())
.includePatterns(".*api.*"); // assuming the API lives at something like http://myapp/api
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("TITLE")
.description("DESCRIPTION")
.version("VERSION")
.termsOfServiceUrl("http://terms-of-services.url")
.license("LICENSE")
.licenseUrl("http://url-to-license.com")
.build();
}
}
当您运行应用程序时,您现在应该会看到在 http://myapp/api-docs 创建的 API 规范。要设置精美的 Swagger UI,您需要从 GitHub project 克隆静态文件并将它们放入您的项目中。确保您的项目配置为提供静态 HTML 文件:
<mvc:resources mapping="*.html" location="/" />
然后在 Swagger UI dist 目录的顶层编辑 index.html 文件。在文件的顶部,您会看到一些 JavaScript 引用了另一个项目的 api-docs URL。编辑它以指向您项目的 Swagger 文档:
if (url && url.length > 1) {
url = url[1];
} else {
url = "http://myapp/api-docs";
}
现在,当您导航到 http://myapp/path/to/swagger/index.html 时,您应该会看到项目的 Swagger UI 实例。