Swagger的使用

我这里使用的是Swagger2
Swagger: 一款RESTFUL接口的文档在线自动生成+功能测试功能软件

配置步骤

1. 配置依赖

有两种方法:(选一种即可,我用的是第一种)
1. 使用第三方依赖

1.1

<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
    <version>1.9.0.RELEASE</version>
</dependency>

1.2
在Spring Boot项目的启动类上添加@EnableSwagger2


2. 使用官方依赖

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.2.2</version>
</dependency>
<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.2.2</version>
</dependency>
<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.13</version>
 </dependency>

2. 配置SwaggerConfig类

package com.jyk.bookkeeping.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * ClassName: SwaggerConfig
 * Description: Swagger配置
 * date: 2019/11/26 22:04
 *
 * @author jingyuankui
 * @since JDK 1.8
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * Swagger文档的 信息
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("API接口文档")   // Swagger标题
                .description("jyk的Swagger文档")           // 标题下的描述
                .version("1.0.0")                           // 版本号
                .build();
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.jyk.bookkeeping"))   // Swagger扫描的包
                .paths(PathSelectors.any())
                .build();
    }
}

4. 设置API文档的路由:

在application.yml中配置
springfox.documentation.swagger.v2.path: /api-docs
对应的路径就是
http://localhost:8081/swagger-ui.html
(我这里用了8081端口)

5. 启动工程并访问

http://localhost:8081/swagger-ui.html
在这里插入图片描述

6. 附录

@Api
valueurl的路径值
tags如果设置这个,value的值会被覆盖
description对API资源的描述
position显示顺序
authorizations高级特性认证时配置
hidden配置为true,将在文档中隐藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值