springboot开启swagger功能

swagger作为一款不错的api查看工具在spring框架项目上使用十分广泛,使用界面如下图所示:

springboot开启swagger功能_第1张图片

具体使用方法:

1.添加以下依赖

        
            io.springfox
            springfox-swagger2
            x.y.z
        

        
            io.springfox
            springfox-swagger-ui
            x.y.z
        

2.创建Swagger配置类

@Configuration
@EnableSwagger2
public class Swagger2Config {
    
    @Bean
    public Docket createRestApi() {
            docket = new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.goldmantis.controller"))
                    .paths(PathSelectors.any())
                    .build();
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("利用Swagger2构建RESTful APIs")
                .description("swagger demo")
                .termsOfServiceUrl("")
                .contact(new Contact("zhang san","",""))
                .version("1.0")
                .build();
    }

}

3.输入url访问,url拼接路径为:

http://ip:port/contextPath/swagger-ui.html,比如http://localhost:8080/swagger-ui.html

因为springboot默认可以使用/static/,/public/,/resources,/META-INF/resources目录下的静态资源文件,而swagger-ui插件提供的swagger-ui.html在/META-INF/resources下,如下图所示:

springboot开启swagger功能_第2张图片

你可能感兴趣的:(spring,boot)