Swagger2 的使用

1.配置依赖

<!--swagger2 依赖-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

使用doc文档:http://localhost:8080/doc.html

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.2</version>
</dependency>

2.配置类

package com.ecc.swagger2.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

/**
 * @author sunyc
 * @create 2022-07-07 8:48
 */
    /**
     * Swagger2配置类
     * 在与spring boot集成时,放在与Application.java同级的目录下。
     * 通过@Configuration注解,让Spring来加载该类配置。
     * 再通过@EnableSwagger2注解来启用Swagger2。
     */
@Configuration
@EnableSwagger2
public class Swagger12 {
        //读取配置文件中的enable,true为显示,false为隐藏


        @Bean
        public Docket createDocke(){
            return new Docket(DocumentationType.SWAGGER_2)
                    //进入swagger-ui的信息
                    .apiInfo(apiInfo())
                    .select()
                    //暴露所有controller类的所在的包路径
                    .apis(RequestHandlerSelectors.basePackage("com.ecc.swagger2.controller"))
                    .paths(PathSelectors.any())
                    .build()
                    .enable(true);
        }
        //进入swagger-ui的信息
        private ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    //该项目的名字
                    .title("Spring Boot 2.x教程")
                    //该项目的描述
                    .description("spring boot2.x 描述")
                    .version("1.0")
                    .build();
        }



}

3.启动路径:http://localhost:8080/swagger-ui.html

相关注解:

  1. //接口模块描述信息

  2. @Api(tags = "用户模块",description = "用户相关模块接口")

  3. //接口描述信息

  4. @ApiOperation(value = "获取用户详情")

  5. //给Swagger-UI中的输入框中设置默认值

  6. @ApiImplicitParam(name = "id",value = "用户id",defaultValue = "123456")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值