Swagger常用参数用法

Easter79
• 阅读 332

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

1、常用参数

a、配置参数

 1 package com.mao.swagger.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 
 6 import springfox.documentation.builders.PathSelectors;
 7 import springfox.documentation.builders.RequestHandlerSelectors;
 8 import springfox.documentation.spi.DocumentationType;
 9 import springfox.documentation.spring.web.plugins.Docket;
10 import springfox.documentation.swagger2.annotations.EnableSwagger2;
11 
12 @Configuration
13 @EnableSwagger2
14 public class SwaggerConfig {
15     
16     @Bean
17     public Docket userDocket() {
18         return new Docket(DocumentationType.SWAGGER_2)
19                 .groupName("商品接口文档")
20                 .select()
21                 .paths(PathSelectors.any())
22                 .apis(RequestHandlerSelectors.basePackage("com.mao.swagger.goods.controller"))
23                 .build();
24     }
25     
26     @Bean
27     public Docket deviceDocket() {
28         return new Docket(DocumentationType.SWAGGER_2)
29                 .groupName("用户接口文档")
30                 .select()
31                 .paths(PathSelectors.any())
32                 .apis(RequestHandlerSelectors.basePackage("com.mao.swagger.user.controller"))
33                 .build();
34     }
35     
36 }
  • .groupName("商品接口文档")   设置栏目名
  • .select()  初始化并返回一个API选择构造器
  • .paths(PathSelectors.any())   设置路径筛选
  • .apis(RequestHandlerSelectors.basePackage("com.mao.swagger.goods.controller"))  添加路径选择条件
  • .build();    构建

PathSelectors类的方法:

  Predicate any():满足条件的路径,该断言总为true

  Predicate none():不满足条件的路径,该断言总为false  (生产环境可以屏蔽掉swagger:https://blog.csdn.net/goldenfish1919/article/details/78280051)

  Predicate regex(final String pathRegex):符合正则的路径

RequestHandlerSelectors类的方法:

  Predicate any():返回包含所有满足条件的请求处理器的断言,该断言总为true

  Predicate none():返回不满足条件的请求处理器的断言,该断言总为false

  Predicate 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

 1 package com.mao.swagger.user.controller;
 2 
 3 import org.springframework.http.HttpStatus;
 4 import org.springframework.http.MediaType;
 5 import org.springframework.web.bind.annotation.RequestBody;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 import org.springframework.web.bind.annotation.RequestParam;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 import com.mao.swagger.beans.ResObject;
12 import com.mao.swagger.beans.User;
13 
14 import io.swagger.annotations.Api;
15 import io.swagger.annotations.ApiImplicitParam;
16 import io.swagger.annotations.ApiImplicitParams;
17 import io.swagger.annotations.ApiOperation;
18 import springfox.documentation.annotations.ApiIgnore;
19 
20 /**
21  * Hello world!
22  *
23  */
24 @Api(description = "用户接口")
25 @RestController
26 @RequestMapping("/userController")
27 public class UserController {
28 
29     @ApiOperation(value = "新增用户" ,  notes="新增注册")
30     @RequestMapping(value="/createUser",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
31     public ResObject createUser(@RequestBody User user){
32         System.out.println("createUser:::"+user.toString());
33         return new ResObject(HttpStatus.OK.value(), "新增成功.");
34     }
35 
36     @ApiOperation(value = "修改用户" ,  notes="修改用户")
37     @RequestMapping(value="/updateUser",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
38     public ResObject updateUser(@RequestBody User user){
39         System.out.println("updateUser:::"+user.toString());
40         return new ResObject(HttpStatus.OK.value(), "修改成功.");
41     }
42 
43     @ApiOperation(value = "删除用户" ,  notes="删除用户")
44     @ApiImplicitParams({
45         @ApiImplicitParam(name = "userId", value = "用户标识", required = true, paramType = "query", dataType = "String")
46     })
47     @RequestMapping(value="/deleteUser",method=RequestMethod.DELETE)
48     public ResObject deleteUser(@RequestParam("userId") String userId){
49         System.out.println("deleteUser:::"+userId);
50         return new ResObject(HttpStatus.OK.value(), "删除成功.");
51     }
52 
53     @ApiOperation(value = "查询用户" ,  notes="查询用户")
54     @ApiImplicitParam(name = "userId", value = "用户标识", required = true, paramType = "query", dataType = "String")
55     @RequestMapping(value="/queryUser",method=RequestMethod.GET)
56     public ResObject queryUser(@RequestParam("userId") String userId){
57         System.out.println("queryUser:::"+userId);
58         User user = new User(userId, "张三", "******", "mao2080@sina.com");
59         return new ResObject(HttpStatus.OK.value(), user);
60     }
61     
62     @ApiOperation(value = "被遗忘的方法" ,  notes="这个方法将被不会显示")
63     @ApiIgnore
64     @ApiImplicitParam(name = "userId", value = "用户标识", required = true, paramType = "query", dataType = "String")
65     @RequestMapping(value="/queryUser1",method=RequestMethod.GET)
66     public ResObject queryUser1(@RequestParam("userId") String userId){
67         System.out.println("queryUser:::"+userId);
68         User user = new User(userId, "张三", "******", "mao2080@sina.com");
69         return new ResObject(HttpStatus.OK.value(), user);
70     }
71 
72 }

 效果图1

Swagger常用参数用法

code2

 1 package com.mao.swagger.beans;
 2 
 3 import io.swagger.annotations.ApiModel;
 4 import io.swagger.annotations.ApiModelProperty;
 5 
 6 @ApiModel(value="user", description="用户对象")
 7 public class User {
 8     
 9     @ApiModelProperty(value="用户id",name="userId",example="001")
10     private String userId;
11     
12     @ApiModelProperty(value="用户名",name="userName",example="mao2080")
13     private String userName;
14     
15     @ApiModelProperty(value="密码",name="password",example="123456")
16     private String password;
17     
18     @ApiModelProperty(value="邮箱",name="email",example="mao2080@sina.com")
19     private String email;
20 
21     public User() {
22         super();
23     }
24 
25     public User(String userId, String userName, String password, String email) {
26         super();
27         this.userId = userId;
28         this.userName = userName;
29         this.password = password;
30         this.email = email;
31     }
32 
33     public String getUserId() {
34         return userId;
35     }
36 
37     public void setUserId(String userId) {
38         this.userId = userId;
39     }
40 
41     public String getUserName() {
42         return userName;
43     }
44 
45     public void setUserName(String userName) {
46         this.userName = userName;
47     }
48 
49     public String getPassword() {
50         return password;
51     }
52 
53     public void setPassword(String password) {
54         this.password = password;
55     }
56 
57     public String getEmail() {
58         return email;
59     }
60 
61     public void setEmail(String email) {
62         this.email = email;
63     }
64 
65     @Override
66     public String toString() {
67         return "User [userId=" + userId + ", userName=" + userName + ", password=" + password + ", email=" + email+"]";
68     }
69 
70 }

效果图2

Swagger常用参数用法

 效果图3

 Swagger常用参数用法

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操作步骤

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Easter79 Easter79
2年前
Swift项目兼容Objective
!456.jpg(http://static.oschina.net/uploads/img/201509/13172704_1KcG.jpg"1433497731426906.jpg")本文是投稿文章,作者:一叶(博客(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2F00red
Stella981 Stella981
2年前
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法参考文章:(1)Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.codeprj.com%2Fblo
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
4个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k