原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9959844.html

SpringBoot整合Swagger2

步骤

第一步:添加必要的依赖

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>

第二步:添加必要的配置

一般无配置项,必要时可以添加自定义配置项,在配置类中读取

第三步:添加配置类(重点)

// swagger2的配置内容仅仅就是需要创建一个Docket实例
@Configuration
@EnableSwagger2 //启用swagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.springbootdemo"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboordemo")
.description("Springboot整合Demo")
.version("0.0.1")
.build(); // 这部分信息其实可以自定义到配置文件中读取
}
}

通过@Configuration注解,让Spring-boot来加载该类配置。再通过@EnableSwagger2注解来启用Swagger2Config。

再通过createRestApi方法创建Docket的Bean之后,

apiInfo方法用来创建该Api的基本信息(这些基本信息会展现在文档页面中)。

select() 函数返回一个 ApiSelectorBuilder 实例用来控制哪些接口暴露给Swagger2来展现。

一般采用指定扫描的包路径来定义

Swagger会扫描该包下所有Controller定义的API,并产生文档内容(除了被@ApiIgnore指定的请求)

第四步:在Controller和Bean上添加Swagger注解

@RestController
@RequestMapping("/user")
@Log4j2
@Api(description = "用户接口")
public class UserApi { @Autowired
private UserService service; @ApiOperation(value = "添加用户", notes = "根据给定的用户信息添加一个新用户",response = ResponseEntity.class,httpMethod = "PATCH")
@RequestMapping(value = "/addUser",method = RequestMethod.PATCH)
public ResponseEntity<User> addUser(final User user) {
log.info("执行添加用户操作");
return service.addUser(user);
} @ApiOperation(value = "更新用户状态", notes = "根据给定的用户ID修改用户状态",response = ResponseEntity.class,httpMethod = "POST")
@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
public ResponseEntity<User> updateUser(final UseState useState, int useId) {
log.info("执行修改用户状态操作");
return service.updateUser(User.builder().useState(useState).useId(useId).build());
} @ApiOperation(value = "更新用户手机号", notes = "根据给定的用户ID修改用户手机号",response = ResponseEntity.class,httpMethod = "POST")
@RequestMapping(value = "/updateUsePhoneNum", method = RequestMethod.POST)
public ResponseEntity<User> updateUsePhoneNum(final String usePhoneNum, int useId) {
log.info("执行修改用户手机号操作");
return service.updateUsePhoneNum(User.builder().usePhoneNum(usePhoneNum).useId(useId).build());
} @ApiOperation(value = "删除用户", notes = "根据给定的用户ID删除一个用户",response = ResponseEntity.class,httpMethod = "DELETE")
@RequestMapping(value = "/deleteUser", method = RequestMethod.DELETE)
public ResponseEntity<User> deleteUser(final int useId) {
log.info("执行删除用户操作");
return service.deleteUser(useId);
} @ApiOperation(value = "查询用户", notes = "根据给定的用户ID获取一个用户",response = ResponseEntity.class,httpMethod = "GET")
@RequestMapping(value = "getUser", method = RequestMethod.GET)
public ResponseEntity<User> getUser(final int useId) {
log.info("执行查询单个用户操作");
return service.getUser(useId);
} @ApiOperation(value = "查询用户", notes = "根据给定的用户信息查询用户",response = ResponseEntity.class,httpMethod = "POST")
@RequestMapping(value = "getUsers", method = RequestMethod.POST)
public ResponseEntity<List<User>> getUsers(final User user) {
log.info("根据条件查询用户");
return service.getUsers(user);
} }
@ApiModel(value = "用户模型")
public class User {
@ApiModelProperty("用户ID")
private int useId;
@ApiModelProperty("用户姓名")
private String useName;
@ApiModelProperty("用户性别")
private UseSex useSex;
@ApiModelProperty("用户年龄")
private int useAge;
@ApiModelProperty("用户身份证号")
private String useIdNo;
@ApiModelProperty("用户手机号")
private String usePhoneNum;
@ApiModelProperty("用户邮箱")
private String useEmail;
@ApiModelProperty("创建时间")
private LocalDateTime createTime;
@ApiModelProperty("修改时间")
private LocalDateTime modifyTime;
@ApiModelProperty("用户状态")
private UseState useState;
}

第五步:启动应用,浏览器请求

http://localhost:8080/swagger-ui.html

可得到如下界面:

SpringBoot整合系列-整合Swagger2的更多相关文章

  1. SpringBoot整合系列-整合MyBatis

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9971036.html SpringBoot整合Mybatis 步骤 第一步:添加必要的j ...

  2. SpringBoot整合系列--整合MyBatis-plus

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/10125279.html SpringBoot整合MyBatis-plus 步骤 第一步: ...

  3. SpringBoot整合系列-整合SpringMVC

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9984607.html SpringBoot整合Spring MVC 步骤 第一步:添加必 ...

  4. SpringBoot整合系列-整合JPA

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9959865.html SpringBoot整合JPA进行数据库开发 步骤 第一步:添加必 ...

  5. SpringBoot整合系列-整合H2

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9959855.html SpringBoot整合H2内存数据库 一般我们在测试的时候习惯于 ...

  6. SpringBoot整合系列-PageHelper分页插件

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9971043.html SpringBoot整合MyBatis分页插件PageHelper ...

  7. SpringBoot系列-整合Mybatis(注解方式)

    目录 一.常用注解说明 二.实战 三.测试 四.注意事项 上一篇文章<SpringBoot系列-整合Mybatis(XML配置方式)>介绍了XML配置方式整合的过程,本文介绍下Spring ...

  8. springboot + mybatis + mycat整合

    1.mycat服务 搭建mycat服务并启动,windows安装参照. 系列文章: [Mycat 简介] [Mycat 配置文件server.xml] [Mycat 配置文件schema.xml] [ ...

  9. SpringBoot与Swagger整合

    1 SpringBoot与Swagger整合https://blog.csdn.net/jamieblue1/article/details/99847744 2 Swagger详解(SpringBo ...

随机推荐

  1. margin与padding的bug

    1.在页面布局时,值对于块元素来说,相邻的两个兄弟块元素间的margin-top与上一个兄弟的margin-bottom重合时, 解决办法:对其中一个块元素中设置    display:inline- ...

  2. MongoDB 用Robomong可视化工具操作的 一些简单语句

    一.数据更新 db.getCollection('表名').update({ "字段":{$in:["值"]} }, //更新条件 {$set:{ " ...

  3. docker pull 镜像报错

    [root@localhost ~]# docker pull ningx Using default tag: latest Trying to pull repository docker.io/ ...

  4. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  5. 不会git的程序员,会不会被鄙视?

    昨天一朋友在微信上问了我一个问题,我觉得很有趣,于是将本次聊天的内容分享给大家. 我朋友说,如果一个程序员不会使用 git,会不会被别人觉得低一个档次? 事先声明啊,这与公司技术栈无关,不要说有些公司 ...

  6. 小程序----选择地理位置 ( wx.chooseLocation ) 和 获取地理位置 (wx.getSetting)

    问题来了:假如我第一次使用wx.chooseLocation()获取权限被拒绝,然后使用wx.getSetting()来重新获取权限该怎么做呢? 思路:wx.chooseLocation()有fail ...

  7. 微信小程序学习笔记(一)

    1.新添加页面,找到app.json,在pages中加入写的路径会自动生成文件 2.页面跳转方式,传参数: wx.navigateTo({ url: '../home/home?title=' + a ...

  8. FCC(ES6写法) Exact Change

    设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. cid  ...

  9. [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

  10. [Swift]LeetCode462. 最少移动次数使数组元素相等 II | Minimum Moves to Equal Array Elements II

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...