Swagger是什么?

Swagger是一个RESTFUL 接口的文档在线自动生成和功能测试的框架。利用swagger2的注解可以快速的在项目中构建Api接口文档,并且提供了测试API的功能。

Springboot 整合Swagger2

创建Springboot项目,添加相关依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.7.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>io.springfox</groupId>
  9. <artifactId>springfox-swagger-ui</artifactId>
  10. <version>2.7.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. </dependencies>

项目结构

Swagger2配置类

  1. @Configuration
  2. @EnableSwagger2
  3. public class Swagger2Configration {
  4. /*
  5. * 创建api文档
  6. * */
  7. @Bean
  8. public Docket createRestApi(){
  9. return new Docket(DocumentationType.SWAGGER_2)
  10. .apiInfo(apiInfo())
  11. .select() .apis(RequestHandlerSelectors.basePackage("com.jotal.springboot06swagger2.Controller")) //为该包下的类API文档
  12. .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))//为有@Api注解的类生成API文档
  13. .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //为有@ApiOperation注解的方法生成API文档
  14. .paths(PathSelectors.any())
  15. .build();
  16. }
  17. /*
  18. * api文档展示信息
  19. * */
  20. private ApiInfo apiInfo(){
  21. return new ApiInfoBuilder()
  22. .title("Jotal项目接口文档")
  23. .description("测试api文档")
  24. .contact(new Contact("jotal","jotalngu.github.io",null))
  25. .version("0.1")
  26. .build();
  27. }
  28. }

@Configuration 标注该类为配置类

@EnableSwagger2 启用Swagger2

@Bean 将Docket的实例对象注入Spring ioc容器中

apis 为选择生成Api文档的范围,有三种方式:

  • 生成指定包下面的类的API文档
  • 生成有指定注解的类的API文档
  • 生成有指定注解的方法的API文档

paths(PathSelectors.any()) 表示路径选择器匹配所有路径

实体类

  1. package com.jotal.springboot06swagger2.entity;
  2. import io.swagger.annotations.ApiModel;
  3. import io.swagger.annotations.ApiModelProperty;
  4. @ApiModel(value = "用户信息")
  5. public class User {
  6. @ApiModelProperty(value ="年龄",name = "age")
  7. private int age;
  8. private String name;
  9. //getter、setter、toString和构造器
  10. }

控制类

  1. package com.jotal.springboot06swagger2.Controller;
  2. import com.jotal.springboot06swagger2.entity.User;
  3. import io.swagger.annotations.Api;
  4. import io.swagger.annotations.ApiOperation;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. @RestController
  11. @RequestMapping("/testApi")
  12. @Api(tags = "用户信息接口")
  13. public class UserController {
  14. @GetMapping("/getAllUser")
  15. @ApiOperation(value="查询所有", notes="查询所有用户信息")
  16. public List<User> getAllUser(){
  17. List<User> userList = new ArrayList<>();
  18. for(int i=0;i<4;i++){
  19. User user = new User();
  20. user.setAge(i);
  21. userList.add(user);
  22. }
  23. return userList;
  24. }
  25. }

测试

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

点击Try it out! 就可以测试功能!

springboot笔记08——整合swagger2的更多相关文章

  1. springboot笔记07——整合MyBatis

    前言 Springboot 整合 MyBatis 有两种方式,分别是:"全注解版" 和 "注解.xml混合版". 创建项目 创建Springboot项目,选择依 ...

  2. springboot笔记10——整合Redis

    依赖 <dependencies> <!--web依赖--> <dependency> <groupId>org.springframework.boo ...

  3. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  4. SpringBoot整合Swagger2,再也不用维护接口文档了!

    前后端分离后,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却很 ...

  5. springBoot(12)---整合Swagger2

    Spingboot整合Swagger2 随着互联网技术的发展,一般开发都是前后端分离,那么前端和后端的唯一联系,变成了API接口:API文档变成了前后端开发人员联系的纽带,变得越来越重要,没有API ...

  6. SpringBoot整合系列-整合Swagger2

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

  7. SpringBoot(七):SpringBoot整合Swagger2

    原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...

  8. SpringBoot整合Swagger2

    相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档. 手写Api文档的几个痛点: 文档需 ...

  9. springboot+cloud 学习(四)Zuul整合Swagger2

    前言 在微服务架构下,服务是分散的,怎么把所有服务接口整合到一起是我们需要关注的. 下面举例用zuul作为分布式系统的网关,同时使用swagger生成文档,想把整个系统的文档整合在同一个页面上来说明. ...

随机推荐

  1. PHP 之根据两个经纬度计算距离

    一.函数代码 /** * @param $lng1 * @param $lat1 * @param $lng2 * @param $lat2 * @return float */ function g ...

  2. rabbitmq添加user及vhost

    rabbitmqctl add_vhost /myhost # 添加 vhost rabbitmqctl add_user me me123 # 设置用户和密码 rabbitmqctl set_per ...

  3. rust变量与可变性

    fn main() { //let x = 5; let mut x = 5; //通过const定义常量名称要大写,并且值不可更改 const Y:i32=6; println!("Y i ...

  4. 第06组 Alpha冲刺(1/4)

    队名:福大帮 组长博客链接:https://www.cnblogs.com/mhq-mhq/p/11863075.html 作业博客 :https://edu.cnblogs.com/campus/f ...

  5. 忘记zip密码咋办?python在手密码我有

    整理资料时发现几个 zip 文件的密码忘记了,于是尝试用python暴力破解 首先是读取和解压zip文件,使用 zipfile 库 import zipfile z = zipfile.ZipFile ...

  6. Web.config 文件例子

    php - 如何使用web.config重写IIS UrlRewriteModule中的网页的URL? <?xml version="1.0" encoding=" ...

  7. 进程、线程、IP、端口间关系

    进程.线程.IP.端口间关系 进程是指在系统中正在运行的一个应用程序: 线程是系统分配处理器时间资源的基本单元,或者说进程之内独立执行的一个单元. 对于操作系统而言,其调度单元是线程.一个进程至少包括 ...

  8. nginx反向代理结合apache和php的配置示例

    .前端nginx主配置文件 # cat nginx.conf worker_processes ; #pid logs/nginx.pid; pid /data/www/logs/nginx.pid; ...

  9. datatables:initComplete和drawCallback比较

    drawCallback: 对表的每个绘制事件执行操作非常有用 - 例如,您可能希望使用新显示的数据更新外部控件,或者启用服务器端处理,您可能希望将事件分配给新创建的元素.此回调旨在实现此目的,并将在 ...

  10. phpexcel无法导出的解决方法

    phpexcel无法导出的解决方法 <pre> set_time_limit(0); ini_set("memory_limit","512M"); ...