一、依赖:

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

二、新增配置项:

@Configuration
@EnableSwagger2
public class Swagger2 { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.bosssoft.platform.appframe.rest"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("应用中心springBoot之RestFulApi接口文档")
.version("1.0")
.build();
}
}

通过@Configuration注解,表明它是一个配置类,@EnableSwagger2开启swagger2。apiINfo()配置一些基本的信息。apis()指定扫描的包会生成文档。

三、写生产文档的注解

swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

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

/**
*
* @author Shaw
*
*/
@RestController
@RequestMapping(value = "/rest", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "/rest", tags = "ApplicationRestController", description = "应用管理接口")
public class ApplicationRestController{ @Autowired
private AfaApplicationService applicationService; @ApiOperation("获取所有应用")
@RequestMapping(value="/application",method=RequestMethod.GET)
public List<ApiApplication> getApplications() {
AfaApplication app = new AfaApplication();
app.setIsOpen("");
List<AfaApplication> afaAppList = applicationService.getAfaApplicationList(app);
return BeanUtils.copyList(afaAppList, ApiApplication.class);
} @ApiOperation("根据用户编码获取当前用户应用")
@RequestMapping(value="/application/userCode/{userCode}",method=RequestMethod.GET)
public List<ApiApplication> getApplicationsByUserCode(@PathVariable("userCode")String userCode) {
return BeanUtils.copyList(applicationService.getApplicationsByUserCode(userCode),ApiApplication.class);
} }

启动项目,访问:

http://127.0.0.1:8088/appframe-web/swagger-ui.html

测试的时候,可以直接在这个ui界面上进行测试,或者可以通过postman或者soapui等工具进行测试。

备注可以通过修改依赖,将Ui的测试界面改成bootstrap风格的Api界面

       <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.</version>
</dependency> <dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.</version>
</dependency>

风格如下:

http://127.0.0.1:8088/appframe-web/doc.html

SpringBoot RestFul集成Swagger2的更多相关文章

  1. Springboot Oauth2 集成Swagger2权限验证实战

    Swagger是什么?能干什么?在这就不展开讲解了.本文主要讲解如何集成OAuth2的Password模式权限验证,验证接口是否具有权限. 引入依赖 <dependency> <gr ...

  2. SpringBoot中集成Swagger2

    1.依赖jar <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...

  3. SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

    1.pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...

  4. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

  5. springboot 集成swagger2

    使用Swagger 可以动态生成Api接口文档,在项目开发过程中可以帮助前端开发同事减少和后端同事的沟通成本,而是直接参照生成的API接口文档进行开发,提高了开发效率.这里以springboot(版本 ...

  6. SpringBoot集成Swagger2在线文档

    目录 SpringBoot集成Swagger2在线文档 前言 集成SpringBoot 登录接口文档示例 代码 效果 注解说明 总结 SpringBoot集成Swagger2在线文档 前言 不得不说, ...

  7. 集成 Swagger2 构建强大的 RESTful API 文档

    微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 快过年了,不知道你们啥时候放年假,忙不忙.反正我是挺闲的,所以有时间写 blog.今天给你们带来 SpringBoo ...

  8. Spring Boot2 系列教程 (四) | 集成 Swagger2 构建强大的 RESTful API 文档

    前言 快过年了,不知道你们啥时候放年假,忙不忙.反正我是挺闲的,所以有时间写 blog.今天给你们带来 SpringBoot 集成 Swagger2 的教程. 什么是 Swagger2 Swagger ...

  9. 【java框架】SpringBoot(3) -- SpringBoot集成Swagger2

    1.SpringBoot web项目集成Swagger2 1.1.认识Swagger2 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体 ...

随机推荐

  1. ubuntu 安装包过程中遇到的一个错误解决办法

    错误提示如下: 将会安装下列额外的软件包: libdigest-hmac-perl libqt5test5下列[新]软件包将被安装: libdigest-hmac-perl下列软件包将被升级: lib ...

  2. Kali之aircrack-ng

    本机装好设备及驱动 电脑本机装好Realtek RTL8187 Wireless驱动连接好USB无线驱动 把设备转接给虚拟机 win+R,启动VMware USB Arbitration Servic ...

  3. iptables详解(9):iptables的黑白名单机制

    注意:在参照本文进行iptables实验时,请务必在个人的测试机上进行,因为如果iptables规则设置不当,有可能使你无法连接到远程主机中. 前文中一直在强调一个概念:报文在经过iptables的链 ...

  4. firefox与ie 的javascript区别

    1. Document.form.item 问题     现有问题: 现有代码中存在许多 document.formName.item("itemName") 这样的语句,不能在 ...

  5. bzoj1704

    题解: 贪心 枚举k 然后判断一下是否可行 代码: #include<bits/stdc++.h> using namespace std; ; int n,a[N],b[N],sum,c ...

  6. 【实用工具】google访问不了比较好的解决办法

    一.直接添加ip,74.125.128.(31~167)都可以,不过不稳定 二.不使用.hk的后缀名,使用.tw,co.jp等均可(同IE用户) 三.下载这个脚本,进行安装(右上角点击“install ...

  7. _routing字段介绍

    一个document通过以下公式被路由到该索引下一个特定的分片: shard_num = hash(_routing) % num_primary_shards _routing的默认值是文档的_id ...

  8. 利用Appium Python测试爱壁纸的登录和设置壁纸

    设置壁纸: #coding:utf-8 #Import the common package import os import unittest from appium import webdrive ...

  9. Python基础学习----列表

      name_list=["张无忌","张三丰","张小明","胡歌","夏东海"] #循环输出na ...

  10. New Concept English three(21)

    27W 59 Boxing matches were very popular in England two hundred years ago. In those days, boxers foug ...