springboot 使用 Swagger2 整合api文档

1.在父工程的pom.xml中引入相关的Swagger2依赖

<!-- swagger2 配置 -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.4.0</version> </dependency>
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.4.0</version>
</dependency>
<dependency>
   <groupId>com.github.xiaoymin</groupId>
   <artifactId>swagger-bootstrap-ui</artifactId>
   <version>1.6</version>
</dependency>

2.编写Swagger2类

@Configuration
@EnableSwagger2
public class Swagger2 {
    //访问地址(原路径):http:localhost:8088/swagger-ui.html
    //修饰后的路径:http:localhost:8088/doc.html
    @Bean
    public Docket createRestApi() {
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lzx.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("天天吃货 电商平台")
                .contact(new Contact("lzx",
                        "www.baidu.com",
                        "xxxxxxxxx@qq.com"))
                .description("专门为天天吃货提供的文档API")
                .version("1.0.1")
                .termsOfServiceUrl("www.baidu.com")
                .build();
    }

}

3.运行访问
访问地址(原路径):http:localhost:8088/swagger-ui.html
修饰后的路径:http:localhost:8088/doc.html(ui修饰后的界面)
在这里插入图片描述
4.在一些方法或属性中添加一些中文注释让别人看得懂.
4-1@ApiIgnore标明Api文档中隐藏该接口(在类名上注释)
在这里插入图片描述
4-2@Api注释相应名称(在类名上注释)
在这里插入图片描述
在这里插入图片描述
4-3@@ApiModel(在实体来上注释)
在这里插入图片描述
在这里插入图片描述
4-4@ApiModelProperty(在实体类上的属性注释)
在这里插入图片描述
在这里插入图片描述
4-4@ApiParam(在方法属性上的注释)
在这里插入图片描述
在这里插入图片描述

5.Swgger2也提供了在线请求调试
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值