别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/

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、推广阅读

Springboot集成Swagger操作步骤

Swagger常用参数用法的更多相关文章

  1. C#中Messagebox.Show()常用参数用法详解

    声明:IWin32Window owner   ,  HelpNavigator navigator ,    string keyword 上面的三个参数类型不是很了解.没有做讨论. 等以后了解多了 ...

  2. maven用途、核心概念、用法、常用参数和命令、扩展

    设置问题解决. http://trinea.iteye.com/blog/1290898 本文由浅入深,主要介绍maven的用途.核心概念(Pom.Repositories.Artifact.Buil ...

  3. Production环境中iptables常用参数配置

    production环境中iptables常用参数配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我相信在实际生产环境中有很多运维的兄弟跟我一样,很少用到iptables的这个 ...

  4. cat常用参数详解

    cat常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近,我的一个朋友对linux特别感兴趣,于是我觉得每天交给他一个命令的使用,这样一个月下来也会使用30个命令,基 ...

  5. ls常用参数

    ls常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 玩Linux的老司机们每天都要敲的命令,但是这个鸡蛋的命令还有很多中玩法哟~跟着我一起敲一遍吧!在这里我就列举几个常 ...

  6. python笔记之常用模块用法分析

    python笔记之常用模块用法分析 内置模块(不用import就可以直接使用) 常用内置函数 help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像 ...

  7. mysql 常用命令用法总结积木学院整理版

    一.启动与退出 1.进入MySQL:启动MySQL Command Line Client(MySQL的DOS界面),直接输入安装时的密码即可.此时的提示符是:mysql> 2.退出MySQL: ...

  8. DAX/PowerBI系列 - 查询参数用法详解(Query Parameter)

    PowerBI  - 查询参数用法详解(Query Parameter) 很多人都不知道查询参数用来干啥,下面总结一下日常项目中常用的几个查询参数的地方.(本人不太欢hardcode的东西) 使用查询 ...

  9. ansible常用模块用法

    ansible常用模块用法 2015-07-21 10:25 24458人阅读 评论(1) 收藏 举报  分类: Linux(44)   ansible 版权声明:本文为博主原创文章,未经博主允许不得 ...

随机推荐

  1. Vue实例:vue2.0+ElementUI框架开发pc项目

    开发前准备 vue.js2.0中文,项目所使用的js框架 vue-router,vue.js配套路由 vuex,状态管理 Element,UI框架 1,根据官方指引,构建项目框架 安装vue npm ...

  2. 富文本编辑器--获取JSON

    获取 JSON 格式的内容 可以通过editor.txt.getJSON获取 JSON 格式的编辑器的内容,v3.0.14开始支持,示例如下 <div id="div1"&g ...

  3. PostgreSQL 自增主键

    1.自增主键:2.创建序列 一.使用SERIAL自增主键 create table test_no( id SERIAL primary key, name ) ); 二.创建序列 INCREMENT ...

  4. 免费安装正版Xshell6+Xftp6

    先进入链接获取最新的下载地址 下载地址 填写一下姓名+邮箱,勾选需要哪个就行了 它会发给你指定邮箱一个地址,点击即可下载 安装就不用说了...

  5. Delphi 过程

  6. c++ 实时通信系统(c++socket篇)

    在上一篇简单的介绍了TCP/IP协议,在这一篇我们主要介绍socket的具体实现的函数 第一步首先我们套添加上头文件:(#pragma comment(lib, "WS2_32") ...

  7. iscsi原理

    iscsi原理 一,[名词解释] SCSI:小型计算机系统接口,SCSI作为i输入/输出接口, FC:光纤通道 DAS:直连式存储,指将存储设备通过SCSI接口或光纤通道直接连到一台计算机上. NAS ...

  8. context容器上下文件

    在web项目中想要获取哪个bean,得先得到容器上下文context public class MyLoaderListener extends ContextLoader implements Se ...

  9. STARTUPINFO结构体

    typedef struct _STARTUPINFO { DWORD cb; //包含STARTUPINFO结构中的字节数.如果Microsoft将来扩展该结构,它可用作版本控制手段.应用程序必须将 ...

  10. 【LuoguP5328】[ZJOI2019]浙江省选

    题目链接 题意 给你一堆斜率和纵截距都为正的直线 ,求对于一个条直线是否存在一个 x 使得在这条直线在 x 处能是前 m 大,输出最高能够达到的排名(排名定义为在 x 处严格大于自己的直线条数+1) ...