Swagger中@ApiIgnore注解的使用详解

 更新时间:2021年10月20日 17:08:53   作者:周公解码  
这篇文章主要介绍了Swagger中@ApiIgnore注解的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Swagger @ApiIgnore注解的使用

@ApiIgnore 可以用在类、方法上,方法参数中,用来屏蔽某些接口或参数,使其不在页面上显示。

1、作用在类上时,整个类都会被忽略

@ApiIgnore
@Api(tags = {"Xxx控制类"})
@RestController
@RequestMapping("/xxx")
public class XxxController {
  ......
}

隐藏某个类还可以用@Api注解自带的hidden属性:

@Api(value = "xxx", tags = "xxx",hidden = true)

当hidden为true时,该类隐藏。

2、当作用在方法上时,方法将被忽略

@ApiIgnore
@ApiOperation(value = "xxx", httpMethod = "POST", notes = "xxx")
@ApiImplicitParams({
  @ApiImplicitParam(name = "xxx", value = "xxx", paramType = "query", dataType = "String", required = true)
})
@PostMapping("/xxx")
public Result importCarryEquExcel(String xxx) {
    ......
}

隐藏某个方法还可以用@APIOperation注解自带的hidden属性:

@ApiOperation(value = "xxx", httpMethod = "GET", notes = "xxx",hidden = true)

当hidden为true时,该方法隐藏。

3、作用在参数上时,单个具体的参数会被忽略

public String abc(@ApiIgnore String a, String b, String c){
    return "a" + "b" + "c";
  }

补充:

4、 在实体类中忽略不需要字段的方式

(1)用@ApiModelProperty注解自带的hidden属性:

    @ApiModelProperty(value = "xxxid", required = true,hidden = true)
    private Long id;

(2)使用@JsonIgnore注解:

    @ApiModelProperty(value = "xxxid", required = true)
    @JsonIgnore
    private Long id;

包名:

import  com.fasterxml.jackson.annotation.JsonIgnore;

swagger 注解的使用解析

Swagger简介

由于架构革新,进入了前后端分离,服务端只需提供RESTful API的时代。

而构建RESTful API会考虑到多终端的问题,这样就需要面对多个开发人员甚至多个开发团队。为了减少与其他团队对接的沟通成本,我们通常会写好对应的API接口文档。

从最早开始的word文档,到后续的showdoc,都能减少很多沟通成本,但随之带来的问题也比较麻烦。在开发期间接口会因业务的变更频繁而变动,如果需要实时更新接口文档,这是一个费时费力的工作。

为了解决上面的问题,Swagger应运而生。他可以轻松的整合进框架,并通过一系列注解生成强大的API文档。他既可以减轻编写文档的工作量,也可以保证文档的实时更新,将维护文档与修改代码融为一体,是目前较好的解决方案。

常用注解

  • @Api()用于类;
  • 表示标识这个类是swagger的资源
  • @ApiOperation()用于方法;
  • 表示一个http请求的操作
  • @ApiParam()用于方法,参数,字段说明;
  • 表示对参数的添加元数据(说明或是否必填等)
  • @ApiModel()用于类
  • 表示对类进行说明,用于参数用实体类接收
  • @ApiModelProperty()用于方法,字段
  • 表示对model属性的说明或者数据操作更改
  • @ApiIgnore()用于类,方法,方法参数
  • 表示这个方法或者类被忽略
  • @ApiImplicitParam() 用于方法
  • 表示单独的请求参数
  • @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam

代码示例

1、@Api

@Api(value = "用户博客", tags = "博客接口")
public class NoticeController { 
}

2、@ApiOperation

@GetMapping("/detail")
@ApiOperation(value = "获取用户详细信息", notes = "传入notice" , position = 2)
public R<Notice> detail(Integer id) {
   Notice detail = noticeService.getOne(id);
   return R.data(detail );
}

3、@ApiResponses

@ApiResponses(value = {@ApiResponse(code = 500, msg= "INTERNAL_SERVER_ERROR", response = R.class)})
@GetMapping("/detail")
@ApiOperation(value = "获取用户详细信息", notes = "传入notice" , position = 2)
@ApiResponses(value = {@ApiResponse(code = 500, msg= "INTERNAL_SERVER_ERROR", response = R.class)})
public R<Notice> detail(Integer id) {
   Notice detail = noticeService.getOne(id);
   return R.data(detail );
}

4、@ApiImplicitParams

以分页代码进行展示

IPage<Notice> pages = noticeService.page(Condition.getPage(query), Condition.getQueryWrapper(notice, Notice.class));

@GetMapping("/list")
@ApiImplicitParams({
   @ApiImplicitParam(name = "category", value = "公告类型", paramType = "query", dataType = "integer"),
   @ApiImplicitParam(name = "title", value = "公告标题", paramType = "query", dataType = "string")
})
@ApiOperation(value = "分页", notes = "传入notice", position = 3)
public R<IPage<Notice>> list(@ApiIgnore @RequestParam Map<String, Object> notice, Query query) {
     IPage<Notice> pages = noticeService.page(Condition.getPage(query), Condition.getQueryWrapper(notice, Notice.class));
   return R.data(pages );
}

5、@ApiParam

@PostMapping("/remove")
@ApiOperation(value = "逻辑删除", notes = "传入notice", position = 7)
public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
   boolean temp = noticeService.deleteLogic(Func.toIntList(ids));
   return R.status(temp);
}

6、@ApiModel 与 @ApiModelProperty

@Data
@ApiModel(value = "BladeUser ", description = "用户对象")
public class BladeUser implements Serializable { 
   private static final long serialVersionUID = 1L; 
   @ApiModelProperty(value = "主键", hidden = true)
   private Integer userId;
 
   @ApiModelProperty(value = "昵称")
   private String userName;
 
   @ApiModelProperty(value = "账号")
   private String account;
 
   @ApiModelProperty(value = "角色id")
   private String roleId;
 
   @ApiModelProperty(value = "角色名")
   private String roleName; 
}

7、@ApiIgnore()

@ApiIgnore()
@GetMapping("/detail")
public R<Notice> detail(Integer id) {
   Notice detail = noticeService.getOne(id);
   return R.data(detail );
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • IDEA 自定义方法注解模板的实现方法

    IDEA 自定义方法注解模板的实现方法

    这篇文章主要介绍了IDEA 自定义方法注解模板的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • SpringBoot初始化加载配置的八种方式总结

    SpringBoot初始化加载配置的八种方式总结

    在日常开发时,我们常常需要 在SpringBoot应用启动时执行某一段逻辑,如获取一些当前环境的配置或变量、向数据库写入一些初始数据或者连接某些第三方系统,确认对方可以工作,那么在实现初始化逻辑代码时就需要小心了,所以本文介绍了SpringBoot初始化加载配置的方式
    2024-12-12
  • 详解 Java Maximum redirects (100) exceeded

    详解 Java Maximum redirects (100) exceeded

    这篇文章主要介绍了详解 Java Maximum redirects (100) exceeded的相关资料,需要的朋友可以参考下
    2017-05-05
  • mybatis中${}和#{}取值的区别分析

    mybatis中${}和#{}取值的区别分析

    mybatis中使用sqlMap进行sql查询时,经常需要动态传递参数,在动态SQL解析阶段, #{ } 和 ${ } 会有不同的表现,这篇文章主要给大家介绍了关于mybatis中${}和#{}取值区别的相关资料,需要的朋友可以参考下
    2021-09-09
  • SpringMVC请求流程源码解析

    SpringMVC请求流程源码解析

    这篇文章主要介绍了SpringMVC请求流程源码分析,包括springmvc使用,SpringMVC启动过程及SpringMVC请求过程,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • Java 8函数式接口之Consumer用法示例详解

    Java 8函数式接口之Consumer用法示例详解

    这篇文章主要为大家介绍了Java 8函数式接口之Consumer用法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • SpringCloud_Eureka服务注册与发现基础及构建步骤

    SpringCloud_Eureka服务注册与发现基础及构建步骤

    Eureka服务注册中心,主要用于提供服务注册功能,当微服务启动时,会将自己的服务注册到Eureka Server,这篇文章主要介绍了SpringCloud中Eureka的配置及详细使用,需要的朋友可以参考下
    2023-01-01
  • 浅谈Java并发 J.U.C之AQS:CLH同步队列

    浅谈Java并发 J.U.C之AQS:CLH同步队列

    AQS内部维护着一个FIFO队列,该队列就是CLH同步队列。下面小编来简单介绍下这个队列
    2019-05-05
  • 为什么阿里要慎重使用ArrayList中的subList方法

    为什么阿里要慎重使用ArrayList中的subList方法

    这篇文章主要介绍了为什么要慎重使用ArrayList中的subList方法,subList是List接口中定义的一个方法,该方法主要用于返回一个集合中的一段、可以理解为截取一个集合中的部分元素,他的返回值也是一个List。,需要的朋友可以参考下
    2019-06-06
  • Java并发容器相关知识总结

    Java并发容器相关知识总结

    今天给大家带来的文章是Java并发容器的相关知识,文中有非常详细的介绍,对正在学习Java并发容器的小伙伴们很有帮助,需要的朋友可以参考下
    2021-06-06

最新评论