在Spring Boot中使用Swagger生成接口说明书

Spring Boot中使用Swagger生成接口说明书


1. 添加Swagger依赖
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.5.0</version>
        </dependency>
2. 添加配置类
@Configuration
@EnableSwagger2
public class SwaggerConf {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //basePackage路径为需要扫描的包,就是配置过注解,需要生成文档的包。一般是controller的包名
                .apis(RequestHandlerSelectors.basePackage("com.huo.mybatisplus.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("RESTful API")
                //创建人
                .contact(new Contact("Jeff", "https://test.github.io", "1234@qq.com"))
                //版本号
                .version("1.0")
                //描述
                .description("各接口说明书")
                .build();
    }
}
3. 添加注解及说明
3.1 @Api:用在请求的类上,说明该类的作用

value: 没有什么用,可以不用配置
tags: 这个类的说明

@Api(value = "userController",tags = {"用户信息获取"})
@Controller
public class UserController {
}

在这里插入图片描述

3.2 @ApiOperation:用在请求的方法上

value: 方法的概略说明
notes: 方法的详细说明

@ApiOperation(value = "获取所有的用户",notes = "传递id参数,获取用户信息")

在这里插入图片描述

3.3 @ApiImplicitParams:用在请求的方法上,请求参数说明
@ApiImplicitParams({
      @ApiImplicitParam(name = "telephone", value = "电话号码", paramType = "query", required = true, dataType = "Integer"),
      @ApiImplicitParam(name = "name", value = "姓名", paramType = "query", required = true, dataType = "Integer")
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值