使用swagger2生成文档

1.在pom.xml中添加依赖

<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的配置类SwaggerConfig

@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类上添加文档注释

  通过@ApiOperation注解,来给API增加说明;

  通过@ApiImplicitParams、@ApiImplicitParam注解,来给参数增加说明;

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

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

猜你喜欢

转载自www.cnblogs.com/gxh494/p/11802292.html