使用swagger2生成文档

1.在pom.xml中添加依赖html

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>

2.建立Swagger2的配置类SwaggerConfigspring

@Configuration  //这是一个配置类
@EnableSwagger2 //开启在线文档
public class SwaggerConfig {
    //1.声明api 文档的属性、构建器
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder().title("SpringBoot中使用在线文档构建RestFul风格Apis")
                .description("gxh").contact("gxh").version("1.0").build();
    }

    //2.配置核心配置信息
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select().apis(RequestHandlerSelectors.basePackage("com.offcn.springbootdemo.controller"))
                .paths(PathSelectors.any()).build();
    }
}

3.在Controller包下的Controller类上添加文档注释api

  经过@ApiOperation注解,来给API增长说明;浏览器

  经过@ApiImplicitParams、@ApiImplicitParam注解,来给参数增长说明;springboot

 

4.运行,查看生成的Swagger2文档ui

启动以后,在浏览器中输入:http://localhost:8080/swagger-ui.htmlspa

相关文章
相关标签/搜索