别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处: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. mongoose操作笔记

    一.mongoose文档地址: https://cn.mongoosedoc.top/docs/api.html#update_update https://www.cnblogs.com/web-f ...

  2. 一款完美代替微信小程序原生客服消息的工具:

    一.设置:无需开发,多种回复(自动+人工)   自动回复形式有3种: 打开客服消息(用户只要和客服互动过一次,再次点击进入,会收到设置好的自动回复) 关键词回复(用户在小程序中回复某个关键词内容时,会 ...

  3. MySQL两种内核对比

    MySQL内核 https://blog.csdn.net/baichoufei90/article/details/83504446 关键字:全文索引 索引外置 两种内核:MyISAM 和InnoD ...

  4. CSS-百分百布局

    1.照片随着大小变化: 这里面重点就是每个包裹盒子是25%,图片是100%显示: <div class="box2"> <p> //这里都是4个: < ...

  5. echarts属性的设置

    // 全图默认背景  // backgroundColor: ‘rgba(0,0,0,0)’, // 默认色板 color: ['#ff7f50','#87cefa','#da70d6','#32cd ...

  6. SSM处理 No 'Access-Control-Allow-Origin' header is present on the requested resource 问题

    在开发中,前端同事调用后端同事写好的接口,在地址中是有效的,但在项目的ajax中,浏览器会报 "No 'Access-Control-Allow-Origin' header is pres ...

  7. 第二篇 HTML 常用元素及属性值

    常用元素及属性值 先和同学了解下,一部分常用的元素,区别以及属性,常用在哪里.   标签是由左右尖括号抱起来的,由开始标签开始,再由结束标签结束,里面内容则是元素,比如:<div>< ...

  8. dubbo学习笔记一(服务注册)

    相关的资料 官方文档 官方博客 项目结构 项目说明 [lesson1-config-api] 是一个接口工程,编译后是jar包,被其他工程依赖 [lesson1-config-2-properties ...

  9. js带有遮罩的弹窗

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. 网络初级篇之STP(概念原理)

    一.什么是STP 生成树协议(Spanning Tree Protocol,STP),是一种工作在OSI网络模型中的第二层(数据链路层)的通信协议,基本应用是防止交换机冗余链路产生的环路.用于确保以太 ...