Swagger 是一个规范和完整的框架,用于生成,描述,调用和可视化RESTful风格的web服务

  http://swagger.io

  Springfox的前身是swagger-springmvc,是一个开源的API doc框架,可以将我们的Controller接口的方法以文档的形式展现,基于swagger,这样就方便开发人员不用在开发完接口服务之后还需要手写一份文档给需要的人。

  使用方式如下

  引入依赖:

  pom.xml文件

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

  编写配置类,并使用@EnableSwagger2开启支持swagger2,配置类如下

  

package com.wangx.boot.util;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import static springfox.documentation.builders.PathSelectors.regex; @Configuration
@EnableSwagger2
public class Swagger2Configuration { @Bean
public Docket accessToker() {
return new Docket(DocumentationType.SWAGGER_2).
groupName("api")//定一组
.select()//选择那些路径和接口api会生成document
.apis(RequestHandlerSelectors.basePackage(""))//拦截的包
.paths(regex("/api/.*"))//拦截该路劲下的接口
.build() //创建
          .apiInfo(apiInfo()); //配置说明
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("测试项目")//标题
.description("springboot整合swagger")//描述
.termsOfServiceUrl("http://www.baidu.com")//
.contact(new Contact("wangx", "http://baidu.com","1028106567@qq.com"))//联系人
.version("1.0")//版本
.build();
}
}

  这样就会拦截api路劲下的所有接口并生成document.

  访问:http://localhost:8080/swagger-ui.html#/

  视图如下:

  

  可以查看接口的相关信息,并且也可以不用通过postman就可以测试接口了。相当的便利的。

  同时swagger也提供了一些注解可以让我们使用在类或者方法上

  Swagger的注解及作用。 

  @Api:修饰整个类,描述Controller的作用
  @ApiOperation:描述一个类的一个方法,或者说一个接口
  @ApiParam:单个参数描述
  @ApiModel:用对象来接收参数
  @ApiProperty:用对象接收参数时,描述对象的一个字段
  @ApiResponse:HTTP响应其中1个描述
  @ApiResponses:HTTP响应整体描述
  @ApiIgnore:使用该注解忽略这个API
  @ApiError :发生错误返回的信息
  @ApiImplicitParam:一个请求参数
  @ApiImplicitParams:多个请求参数

SpringBoot学习笔记(16)----SpringBoot整合Swagger2的更多相关文章

  1. SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用

    1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...

  2. springboot学习笔记-5 springboot整合shiro

    shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/  它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和sh ...

  3. springboot学习笔记-6 springboot整合RabbitMQ

    一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...

  4. 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)

    http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...

  5. SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理

    在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...

  6. SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]

    https://blog.csdn.net/a67474506/article/details/52608855 Spring定义了org.springframework.cache.CacheMan ...

  7. SpringBoot学习笔记(15)----SpringBoot使用Druid

    直接访问Druid官网wiki,有详细教程,地址如下: SpringBoot支持Druid地址:https://github.com/alibaba/druid/tree/master/druid-s ...

  8. SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用

    1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...

  9. SpringBoot学习笔记(10)-----SpringBoot中使用Redis/Mongodb和缓存Ehcache缓存和redis缓存

    1. 使用Redis 在使用redis之前,首先要保证安装或有redis的服务器,接下就是引入redis依赖. pom.xml文件如下 <dependency> <groupId&g ...

随机推荐

  1. 一个关于传参数js数组的封装方法(寄生模式)

    function createArr(){ var arr = new Array(); arr.push.apply(arr,arguments); arr.toJoin = function(){ ...

  2. OpenCart 如何安装 vQmod 教程

    vQmod (全称 Virtual Quick Mod),是 OpenCart (PHP 开源电商网站系统)上一个可以以虚拟方式修改原文件内容而设计的一个插件系统.它的使用很简单,我们先用 xml 的 ...

  3. 学Arduino 需要做哪些准备?(引自"知乎用户:郑兴芳,DhP"的回答)

    本人非电子专业,使用Arduino完全出于兴趣,目前主要用于实验过程中的自动化操作. 一.基础准备主要是看一些入门介绍的电子文档,如Arduino_Basic.PDF.ArduinoL2.PDF .& ...

  4. JS面向对像编程四—— prototype 对象

    http://blog.csdn.net/fanwenjieok/article/details/54575560 大部分面向对象的编程语言,都是以“类”(class)作为对象体系的语法基础.Java ...

  5. 关于背景颜色、TEXT、<b>、<i>、<u>、<br>、<&nbsp>、<br>、<br>、h1-h6、<span>、<div>、<ol>、<ul>、<a>标签的用法(下载、跳转、锚点)、Img插入的用法

    <html>    <head>        <meta charset="UTF-8">        <title></ ...

  6. 基于cxf的webService服务发布及客户端开发

    学习地址: http://www.cnblogs.com/leihenqianshang/category/795140.html

  7. PHP SOAP 使用示例

    soap_client.php <?php try { $client = new SoapClient( null, array('location' =>"http://lo ...

  8. CentOS安装记录

    决定开始复习Linux系统编程,这次我不再折腾Linux下的各种工具,直接使用VS2017进行代码编写与调试. 配置项 值 VM VMware® Workstation 15 Pro OS CentO ...

  9. Swoole 源码分析——进程管理 Swoole_Process

    前言 swoole-1.7.2 增加了一个进程管理模块,用来替代 PHP 的 pcntl 扩展. PHP自带的pcntl,存在很多不足,如 pcntl 没有提供进程间通信的功能 pcntl 不支持重定 ...

  10. python基础知识部分练习大全

    python基础知识部分练习大全   1.执行 Python 脚本的两种方式 答:1.>>python ../pyhton.py 2. >>python.py   #必须在首行 ...