springboot 整合Swagger

Swagger 简介

  • 最流行的APi框架

  • APi自动生成工具=> Api文档与Api定义同步更新

SpringBoot 集成Swagger

1.新建一个SpringBoot Web 项目

2.导入Swagger的依赖

<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>

3.配置Swagger

@Configuration
@EnableSwagger2//开启Swagger
public class SwaggerConfig {    }

4.测试运行 localhost:8080/swagger-ui.html

在这里插入图片描述

配置Swagger

Swagger 的Bean 实例 Docket

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    //配置Swagger apiInfo
    private ApiInfo apiInfo (){
        Contact contact = new Contact("yang","http:47.111.107.209:8080","1622489328@qq.com");
        return new ApiInfo(
                "public_c_lass API",
                "写代码就完事了",
                "1.0",
                "http:47.111.107.209:8080",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>()
                );
    }
}

配置展示:

在这里插入图片描述

Swagger 配置扫描接口

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors 配置扫描接口的方式
                //指定要扫描的包
                //basePackage :  指定扫描的包
                //any :扫描全部
                //none : 不扫描
                //withMethodAnnotation : 扫描方法上的注解,参数是一个注解的反射现象
                //withClassAnnotation : 扫描类上的注解
                .apis(RequestHandlerSelectors.basePackage("com.example.department"))
                //paths: 过滤什么路径
//                .paths(PathSelectors.ant("/example/**"))
                .build();
    }

    //配置Swagger apiInfo
    private ApiInfo apiInfo (){
        Contact contact = new Contact("yang","http:47.111.107.209:8080","1622489328@qq.com");
        return new ApiInfo(
                "public_c_lass API",
                "写代码就完事了",
                "1.0",
                "http:47.111.107.209:8080",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>()
                );
    }
}

APi文档的分组

.groupName("public_c_lass API")

创建多个Docket 来实现配置多个组

实体类配置

//只要接口中返回值中存在实体类,Swagger就会把该实体类加入
    @GetMapping("/user")
    public User user(@PathVariable("id") Integer id){
        //查询所有部门信息
        employeeBaseMapper.deleteById(id);
        return new User();
    }
@ApiModel("用户实体类")
@Component
@ConfigurationProperties(prefix = "user")
public class User {
    @ApiModelProperty("用户Id")
    private Integer id;
    @ApiModelProperty("用户名")
    private String userName;
    private String passWord;
    private String realName;
}

在这里插入图片描述

 //只要接口中返回值中存在实体类,Swagger就会把该实体类加入
    @ApiOperation("用户控制类")
    @GetMapping("/user1")
    public User user1(@ApiParam("用户id") @PathVariable("id") Integer id){
        //查询所有部门信息
        employeeBaseMapper.deleteById(id);
        return new User();
    }

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值