J-框架-springboot-Swagger

Swagger

Restful Api 文档在线自动生成器 => API 文档 与API 定义同步更新。直接运行,在线测试API。
使用
springboot集成Swagger

1.导入依赖

<!--整合Swagger-->
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>

2.集成Swagger

package com.spring_01.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
   
}

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

3.配置Swagger

package com.spring_01.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiListing;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {

    //配置文档信息
    private ApiInfo apiInfo(){
        Contact contact = new Contact("联系人名字", "http://xxx.xxx.com/联系人访问链接", "联系人邮箱");
        return new ApiInfo(
          "Swagger学习", //标题
          "配置Swagger",//描述
          "v1.0",//版本
          "http://baidu.com",//组织连接
          contact,//联系人信息
          "Apach 2.0许可",//许可
          "许可连接",//许可连接
          new ArrayList<>()
        );
    }

    //配置docket,配置具体Swagger参数
    //Docket实例关联上配置信息
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }

}

配置结果:
在这里插入图片描述

4.配置扫描接口

配置哪些接口可以被扫描哪些不能被扫描

//配置docket,配置具体Swagger参数
    //Docket实例关联上配置信息
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors
                        //扫描方法
                        //.basePackage("com.kuang.swagger.controller")//扫描包
                        //any() // 扫描所有,项目中的所有接口都会被扫描到
                        //none() // 不扫描接口
                        //withMethodAnnotation(GetMapping.class)// 通过方法上的注解扫描,如只扫描get请求
                        //.withClassAnnotation(Controller.class)//通过类上的注解扫描,如只扫描有controller注解的类中的接口
                        //basePackage(final String basePackage) // 根据包路径扫描接口
                        .withMethodAnnotation(GetMapping.class))
                //配置如何通过path过滤,
                .paths(PathSelectors
                        //.ant("/kuang/**"))//只扫描请求以/kuang开头的接口
                        //any() // 任何请求都扫描
                        //none() // 任何请求都不扫描
                        //regex(final String pathRegex) // 通过正则表达式控制
                        .ant("/kuang/**"))
                .build();
    }

5.配置Swagger开关

1、通过enable()方法配置是否启用swagger,如果是false,swagger将不能在浏览器中访问了。
在这里插入图片描述
当开发完成后上线时,可以将swagger开关改为false。

6,.配置API分组

1、如果没有配置分组,默认是default。通过groupName()方法即可配置分组。

@Bean
public Docket docket(Environment environment) {
   return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
      .groupName("hello") // 配置分组
       // 省略配置....
}

2.多个配置分组

  //分组配置
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group1");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group2");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group3");
    }

结果:
在这里插入图片描述

7.配置实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("用户实体")
public class Account {

    @ApiModelProperty("用户id")
    private int id;
    @ApiModelProperty("用户名")
    private String name;
    @ApiModelProperty("账户金额")
    private String money;
}

结果:
在这里插入图片描述

@RestController
public class AccountController {

    @Autowired
    AccountMapper accountMapper;

    //查询全部账户
    @ApiOperation("查询全部账户")
    @GetMapping("/queryAccountList")
    public List<Account> queryAccountList(){

        System.out.println("进入controller了-------------");

        List<Account> accountList = accountMapper.queryAccountList();
        System.out.println("打印结果"+accountList);

        return accountMapper.queryAccountList();
    }

    //查询账户
    @ApiOperation("查询账户")
    @PostMapping("/queryAccountById")
    public Account queryAccountById(){

        System.out.println("进入controller了-------------");
        return accountMapper.queryAccountById(3);
    }

    //添加账户
    @ApiOperation("添加账户")
    @GetMapping("/addAccount")
    public String addAccount(){

        Account account = new Account(10,"测试人员","100");
        System.out.println("进入controller了-------------");
        return "结果:"+accountMapper.addAccount(account);
    }

}

结果:
在这里插入图片描述
注:并不是因为@ApiModel这个注解让实体显示在这里了,而是只要出现在接口方法的返回值上的实体都会显示在这里,而@ApiModel和@ApiModelProperty这两个注解只是为实体添加注释的。

@ApiModel为类添加注释

@ApiModelProperty为类属性添加注释

Swagger注解 简单说明
①:@Api(tags = “xxx模块说明”) 作用在模块类上
②:@ApiOperation(“xxx接口说明”) 作用在接口方法上
③:@ApiModel(“xxxPOJO说明”) 作用在模型类上:如VO、BO
④:@ApiModelProperty(value = “xxx属性说明”,hidden = true) 作用在类方法和属性上,hidden设置为true可以隐藏该属性
⑤:@ApiParam(“xxx参数说明”) 作用在参数、方法和字段上,类似@ApiModelProperty

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
整理自尚硅谷视频教程springboot高级篇,并增加部分springboot2.x的内容 一、Spring Boot与缓存 一、JSR107 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry。 • CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可 以在运行 期访问多个CachingProvider。 • CacheManager定义了创建、配置、获取、管理和控制多个唯一命名 的Cache,这些Cache 存在于CacheManager的上下文中。一个CacheManager仅被一个 CachingProvider所拥有。 • Cache是一个类似Map的数据结构并临时存储以Key为索引的值。一个 Cache仅被一个 CacheManager所拥有。 • Entry是一个存储在Cache中的key-value对。 • Expiry 每一 个存储在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目为过期 的状态。一旦过期,条 目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。 二、Spring缓存抽象 Spring从3.1开始定义了org.springframework.cache.Cache 和 org.springframework.cache.CacheManager接口来统一不同的缓存技术; 并支持使用JCache(JSR- 107)注解简化我们开发; • Cache接口为缓存的组件规范定义,包含缓存的各种操作集合; • Cache接 口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache , ConcurrentMapCache 等; • 每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否 已经被调用 过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法 并缓存结果后返回给用户。下 次调用直接从缓存中获取。 • 使用Spring缓存抽象时我们需要关注以下两点; 1、确定方法需要被缓存 以及他们的缓存策略 2、从缓存中读取之前缓存存储的数据 Cache 缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、 ConcurrentMapCache等 CacheManager 缓存管理器,管理各种缓存(Cache)组件 @Cacheable 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @CacheEvict 清空缓存 @CachePut 保证方法被调用,又希望结果被缓存。 @EnableCaching 开启基于注解的缓存 keyGenerator 缓存数据时key生成策略 serialize 缓存数据时value序列化策略 @CacheConfig 抽取缓存的公共配置 三、几个重要概念&缓存注解 1、常用注解 2、常用参数 名字 位置 描述 示例 methodName root object 当前被调用的方法名 #root.methodName method root object 当前被调用的方法 #root.method.name target root object 当前被调用的目标对象 #root.target targetClass root object 当前被调用的目标对象类 #root.targetClass args root object 当前被调用的方法的参数列表 #root.args[0] 3、常用参数SPEL说明 名字 位置 描述 示例 caches root object 当前方法调用使用的缓存列表(如 @Cacheable(value= {"cache1","cache2"}) ), 则有两 个cache #root.caches[0].name argument name evaluation context 方法参数的名字. 可以直接 #参数 名 ,也可以使用 #p0或#a0 的形 式,0代表参数的索引; #iban 、 #a0 、 #p0 result evaluation context 方法执行后的返回值(仅当方法执 行之后的判断有效,如‘unless’ , ’cache put’的表达式 ’cache evict’的表达式 beforeInvocation=false ) #result 四、代码中使用缓存 1、搭建基本环境 1、导入数据库文件 创建出department和employee表 2、创建javaBean封装数据 3、整合MyBatis操作数据库 1.配置数据源信息 2.使用注解版的MyBatis; 1)、@MapperScan指定需要扫描的mapper接口所在的包

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值