swagger实用接口文档生成框架

来源:互联网 发布:手机淘宝买家实名认证 编辑:程序博客网 时间:2024/05/22 09:39

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

springfox的使用如下:引用依赖:<dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger2</artifactId>    <version>2.5.0</version></dependency> 创建一个swagger的配置文件:@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-ui视图,我们需要引用依赖:<dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger-ui</artifactId>    <version>2.5.0</version></dependency>在项目中创建:@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/");    }}在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

0 0
原创粉丝点击