swagger实用接口文档生成框架

现在我们用springfox来代替swagger-springmvc的方式实现,在springmvc-swagger模式的话需要把swagger-ui下的视图文件copy到我们项目的静态资源的目录下,
而用springfox就不用把swagger-ui下的视图文件copy到我们项目的静态资源的目录下面了。

springfox的使用如下:
引用依赖:

    io.springfox
    springfox-swagger2
    2.5.0
 

创建一个swagger的配置文件:
@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-ui视图,我们需要引用依赖:
<dependency>
    <groupId>io.springfoxgroupId>
    <artifactId>springfox-swagger-uiartifactId>
    <version>2.5.0version>
dependency>

在项目中创建:
@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/");
    }
}

在springfoxjar包里面在路径classpath:/META-INF/resources/下会有一个html页面swagger-ui.html
这个页面就是我们最后把我们有相应接口标记注释的地方生成html页面的模板框架页面。

参考:[1]. http://stackoverflow.com/questions/26720090/a-simple-way-to-implement-swagger-in-a-spring-mvc-application

[2]. https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X
[3]. http://springfox.github.io/springfox/docs/current/#overriding-property-datatypes

你可能感兴趣的:(SpringMVC,工具使用)