Swagger常用参数用法
1、常用参数
a、配置参数
package com.mao.swagger.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket userDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("商品接口文档")
.select()
.paths(PathSelectors.any())
.apis(RequestHandlerSelectors.basePackage("com.mao.swagger.goods.controller"))
.build();
} @Bean
public Docket deviceDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("用户接口文档")
.select()
.paths(PathSelectors.any())
.apis(RequestHandlerSelectors.basePackage("com.mao.swagger.user.controller"))
.build();
} }
- .groupName("商品接口文档") 设置栏目名
- .select() 初始化并返回一个API选择构造器
- .paths(PathSelectors.any()) 设置路径筛选
- .apis(RequestHandlerSelectors.basePackage("com.mao.swagger.goods.controller")) 添加路径选择条件
- .build(); 构建
PathSelectors类的方法:
Predicate<String> any():满足条件的路径,该断言总为true
Predicate<String> none():不满足条件的路径,该断言总为false (生产环境可以屏蔽掉swagger:https://blog.csdn.net/goldenfish1919/article/details/78280051)
Predicate<String> regex(final String pathRegex):符合正则的路径
RequestHandlerSelectors类的方法:
Predicate<RequestHandler> any():返回包含所有满足条件的请求处理器的断言,该断言总为true
Predicate<RequestHandler> none():返回不满足条件的请求处理器的断言,该断言总为false
Predicate<RequestHandler> basePackage(final String basePackage):返回一个断言(Predicate),该断言包含所有匹配basePackage下所有类的请求路径的请求处理器
b、接口参数
- @Api()用于类; 表示标识这个类是swagger的资源 【参考code1,效果图1】
- @ApiOperation()用于方法; 表示一个http请求的操作 【参考code1,效果图1】
- @ApiParam()用于方法,参数,字段说明; 表示对参数的添加元数据(说明或是否必填等) 【暂时没用,当前使用SpringMVC@RequestParam】
- @ApiModel()用于类 表示对类进行说明,用于参数用实体类接收 【参考code2,效果图2,3】
- @ApiModelProperty()用于方法,字段 表示对model属性的说明或者数据操作更改 【参考code2,效果图2,3】
- @ApiIgnore()用于类,方法,方法参数 表示这个方法或者类被忽略 【参考code1,效果图1】
- @ApiImplicitParam() 用于方法 表示单独的请求参数 【参考code1,效果图1】
- @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam 【参考code1,效果图1】
code1
package com.mao.swagger.user.controller; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import com.mao.swagger.beans.ResObject;
import com.mao.swagger.beans.User; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore; /**
* Hello world!
*
*/
@Api(description = "用户接口")
@RestController
@RequestMapping("/userController")
public class UserController { @ApiOperation(value = "新增用户" , notes="新增注册")
@RequestMapping(value="/createUser",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
public ResObject createUser(@RequestBody User user){
System.out.println("createUser:::"+user.toString());
return new ResObject(HttpStatus.OK.value(), "新增成功.");
} @ApiOperation(value = "修改用户" , notes="修改用户")
@RequestMapping(value="/updateUser",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
public ResObject updateUser(@RequestBody User user){
System.out.println("updateUser:::"+user.toString());
return new ResObject(HttpStatus.OK.value(), "修改成功.");
} @ApiOperation(value = "删除用户" , notes="删除用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户标识", required = true, paramType = "query", dataType = "String")
})
@RequestMapping(value="/deleteUser",method=RequestMethod.DELETE)
public ResObject deleteUser(@RequestParam("userId") String userId){
System.out.println("deleteUser:::"+userId);
return new ResObject(HttpStatus.OK.value(), "删除成功.");
} @ApiOperation(value = "查询用户" , notes="查询用户")
@ApiImplicitParam(name = "userId", value = "用户标识", required = true, paramType = "query", dataType = "String")
@RequestMapping(value="/queryUser",method=RequestMethod.GET)
public ResObject queryUser(@RequestParam("userId") String userId){
System.out.println("queryUser:::"+userId);
User user = new User(userId, "张三", "******", "mao2080@sina.com");
return new ResObject(HttpStatus.OK.value(), user);
} @ApiOperation(value = "被遗忘的方法" , notes="这个方法将被不会显示")
@ApiIgnore
@ApiImplicitParam(name = "userId", value = "用户标识", required = true, paramType = "query", dataType = "String")
@RequestMapping(value="/queryUser1",method=RequestMethod.GET)
public ResObject queryUser1(@RequestParam("userId") String userId){
System.out.println("queryUser:::"+userId);
User user = new User(userId, "张三", "******", "mao2080@sina.com");
return new ResObject(HttpStatus.OK.value(), user);
} }
效果图1

code2
package com.mao.swagger.beans; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; @ApiModel(value="user", description="用户对象")
public class User { @ApiModelProperty(value="用户id",name="userId",example="001")
private String userId; @ApiModelProperty(value="用户名",name="userName",example="mao2080")
private String userName; @ApiModelProperty(value="密码",name="password",example="123456")
private String password; @ApiModelProperty(value="邮箱",name="email",example="mao2080@sina.com")
private String email; public User() {
super();
} public User(String userId, String userName, String password, String email) {
super();
this.userId = userId;
this.userName = userName;
this.password = password;
this.email = email;
} public String getUserId() {
return userId;
} public void setUserId(String userId) {
this.userId = userId;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} @Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", password=" + password + ", email=" + email+"]";
}
69
}
效果图2

效果图3

2、参考网站
https://blog.csdn.net/z28126308/article/details/71126677
https://blog.csdn.net/u014231523/article/details/76522486
https://www.cnblogs.com/softidea/p/6251249.html
3、推广阅读
Swagger常用参数用法的更多相关文章
- C#中Messagebox.Show()常用参数用法详解
声明:IWin32Window owner , HelpNavigator navigator , string keyword 上面的三个参数类型不是很了解.没有做讨论. 等以后了解多了 ...
- maven用途、核心概念、用法、常用参数和命令、扩展
设置问题解决. http://trinea.iteye.com/blog/1290898 本文由浅入深,主要介绍maven的用途.核心概念(Pom.Repositories.Artifact.Buil ...
- Production环境中iptables常用参数配置
production环境中iptables常用参数配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我相信在实际生产环境中有很多运维的兄弟跟我一样,很少用到iptables的这个 ...
- cat常用参数详解
cat常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近,我的一个朋友对linux特别感兴趣,于是我觉得每天交给他一个命令的使用,这样一个月下来也会使用30个命令,基 ...
- ls常用参数
ls常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 玩Linux的老司机们每天都要敲的命令,但是这个鸡蛋的命令还有很多中玩法哟~跟着我一起敲一遍吧!在这里我就列举几个常 ...
- python笔记之常用模块用法分析
python笔记之常用模块用法分析 内置模块(不用import就可以直接使用) 常用内置函数 help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像 ...
- mysql 常用命令用法总结积木学院整理版
一.启动与退出 1.进入MySQL:启动MySQL Command Line Client(MySQL的DOS界面),直接输入安装时的密码即可.此时的提示符是:mysql> 2.退出MySQL: ...
- DAX/PowerBI系列 - 查询参数用法详解(Query Parameter)
PowerBI - 查询参数用法详解(Query Parameter) 很多人都不知道查询参数用来干啥,下面总结一下日常项目中常用的几个查询参数的地方.(本人不太欢hardcode的东西) 使用查询 ...
- ansible常用模块用法
ansible常用模块用法 2015-07-21 10:25 24458人阅读 评论(1) 收藏 举报 分类: Linux(44) ansible 版权声明:本文为博主原创文章,未经博主允许不得 ...
随机推荐
- SpringBoot上传文件,经过spingCloud-Zuul,中文文件名乱码解决办法
最近用springCloud整合springboot做分布式服务发现经过zuul之后上传的中文文件名乱码全都变成?????,从而引发异常,单独用springboot却是好的,在网上找到相关资料总结如下 ...
- Dapper多表查询时子表字段为空
最近在学习使用Dapper时百度了一篇详细使用教程,在做到多表查询的时候,出现如下情况. 使用的SQL如下, SELECT * FROM [Student] AS A INNER JOIN [Juni ...
- 104、验证Swarm数据持久性 (Swarm11)
参考https://www.cnblogs.com/CloudMan6/p/8016994.html 上一节我们成功将 nfs 的volume挂载到 Service上,本节验证 Failover时 ...
- 11 Scrapy框架之递归解析和post请求
一.递归爬取解析多页页面数据 - 需求:将糗事百科所有页码的作者和段子内容数据进行爬取切持久化存储 - 需求分析:每一个页面对应一个url,则scrapy工程需要对每一个页码对应的url依次发起请求, ...
- Centos7:JDK1.8环境配置
1.将压缩包解压缩 tar -zxvf jdk-8u181-linux-x64.tar.gz; 2.配置环境变量 环境变量地址:/etc/profile #set java environment J ...
- React前端有钱途吗?《React+Redux前端开发实战》学起来
再不学React就真的跟不上大前端的形式了,目前几乎所有前端的招聘条件都是精通React者优先,看看拉勾网的React薪资,都是15K-20K,这个暑假,必须动起来了. 如果你熟悉JavaScript ...
- Linux——Session复制中的失败的可能原因之一
组播地址问题 route add -net 224.0.0.0 netmask 240.0.0.0 dev eno16777728(自己的网卡名)
- traceback:让你更加灵活地处理python的异常
异常 异常在python中是屡见不鲜了,程序在执行到某一行代码时,发现有问题,比如数组索引越界,变量没有定义啊等等,此时就会抛出异常 捕获异常 在python,一般都是使用try···except来对 ...
- 关于Java中线程取值并返回的方法
如何让一个线程不断跑起来,并且在取到值的时候能返回值而线程能继续跑呢? 我们都知道可以用Callable接口获得线程的返回值,或者触发事件监听来操作返回值,下面我将介绍另一种方法. public ab ...
- SQLAlchemy中Model.query和session.query(Model)的区别
我们使用Flask 0.11.1,Flask-SQLAlchemy 2.1使用PostgreSQL作为DBMS. 示例使用以下代码更新数据库中的数据: entry = Entry.query.get( ...