Springfox (Swagger spec 2.0, current)
Springfox取代了Swagger-SpringMVC,现在同时支持Swagger规范1.2和2.0。实现类已更改,可以进行一些更深入的自定义,但需要做一些工作。该文档已得到改进,但仍需要为高级配置添加一些详细信息。仍可在下面找到1.2实现的旧答案。
Maven dependency
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.5.0</version></dependency>
最低实现看起来大致相同,但现在使用
Docket类而不是
SwaggerSpringMvcPlugin类:
@Configuration@EnableSwagger2public 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使用杰克逊进行数据绑定。
现在添加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(以前为:)显示UI http://myapp/springfox。如果你没有使用Spring Boot,那么正如yuriy-tumakha在下面的答案中提到的那样,你将需要为文件注册一个资源处理程序。Java配置如下所示:
@Configuration@EnableWebMvcpublic 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/"); }}新的静态文档生成功能看起来也很不错,尽管我自己还没有尝试过。
Swagger-SpringMVC(Swagger spec 1.2,旧版本)
Swagger-SpringMVC的文档可能会有些混乱,但是实际上设置起来非常简单。最简单的配置需要创建一个SpringSwaggerConfigbean并启用基于注释的配置(你可能已经在Spring MVC项目中做到了):
<mvc:annotation-driven/><bean />
但是,我认为值得采取额外的步骤,使用
SwaggerSpringMvcPlugin而不是以前的XML定义的bean 来定义自定义Swagger配置:
@Configuration@EnableSwagger@EnableWebMvcpublic 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(); }}现在,在运行应用程序时,你应该可以在看到创建的API规范http://myapp/api-docs。要设置精美的Swagger UI,你需要从GitHub项目中克隆静态文件,并将其放入你的项目中。确保将你的项目配置为提供静态HTML文件:
<mvc:resources mapping="*.html" location="/" />
然后
index.html在Swagger UI
dist目录的顶层编辑文件。在文件顶部,你将看到一些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实例。



